From c919ca6a52d677d1b69215f48bad8a9236502e60 Mon Sep 17 00:00:00 2001 From: ZivonZhang <466170265@qq.com> Date: Wed, 28 Nov 2018 17:45:05 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=AE=9E=E6=93=8D=E6=BC=94=E7=BB=83/s?= =?UTF-8?q?olutions/t003-zivon.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第三题,时间复杂度O(nlogk) --- .../solutions/t003-zivon.java" | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 "\345\256\236\346\223\215\346\274\224\347\273\203/solutions/t003-zivon.java" diff --git "a/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/t003-zivon.java" "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/t003-zivon.java" new file mode 100644 index 0000000..5372fde --- /dev/null +++ "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/t003-zivon.java" @@ -0,0 +1,113 @@ +import java.util.*; +public class Solution { + + //start + public ArrayList GetLeastNumbers_Solution(int [] input, int k) { + ArrayList ret=new ArrayList(k); + if(input==null || k>input.length ||k==0){ + return ret; + } + + TreeSet min_k_tree=new TreeSet<>(); + + for(int i=0;i result; + public Test(int k,int[] input,ArrayList result){ + this.k = k; + this.input=input; + this.result = result; + } + public Test(){ + } + public void printSelf(){ + System.out.println("正确输出为:"+result); + System.out.println("当前测试用例为:"); + if(input==null){ + System.out.println("空数组引用"); + return; + } + if(input.length==0){ + System.out.println("空数组"); + return; + } + for(int i : input) { + System.out.print(i); + } + System.out.println(" k="+k); + } + } + public static void main(String args[]){ + Solution solution = new Solution(); + int total = 8; + Test[] tests = new Test[total]; + tests[0] = solution.new Test(4,new int[]{4,5,1,6,2,7,3,8},new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4}))); + tests[1] = solution.new Test(0,new int[]{},new ArrayList<>()); + tests[2] = solution.new Test(0,null,new ArrayList<>()); + tests[3] = solution.new Test(1,new int[]{1},new ArrayList<>(Arrays.asList(new Integer[]{1}))); + tests[4] = solution.new Test(0,new int[]{4,5,1,6,2,7,3,8},new ArrayList<>()); + tests[5] = solution.new Test(8,new int[]{4,5,1,6,2,7,3,8},new ArrayList<>(Arrays.asList(new Integer[]{4,5,1,6,2,7,3,8}))); + tests[6] = solution.new Test(8,new int[]{11,33,4,5,1,6,99,384,27,2,7,3,8,10},new ArrayList<>(Arrays.asList(new Integer[]{4,5,1,6,2,7,3,8}))); + tests[7] = solution.new Test(2,new int[]{1,99},new ArrayList<>(Arrays.asList(new Integer[]{1,99}))); + int i = 0; + long runtimeCount = System.currentTimeMillis();//获取当前系统时间(毫秒) + try { + for (i = 0; i < total; i++) { + if(!solution.compare(tests[i].result,solution.GetLeastNumbers_Solution(tests[i].input,tests[i].k))){ + solution.printFail(i*100/total,String.valueOf(solution.GetLeastNumbers_Solution(tests[i].input,tests[i].k))); + tests[i].printSelf(); + return; + } + } + solution.printSuccess(); + int runtime = (int)(System.currentTimeMillis()-runtimeCount); + System.out.println("运行时间为:"+runtime+"ms"); + }catch (Exception e){ + solution.printFail(i*100/total,"异常!"); + tests[i].printSelf(); + System.out.println("出现异常!"); + e.printStackTrace(); + } + } + private void printSuccess(){ + System.out.println("恭喜!你通过了所有的测试用例"); + } + private void printFail(int percent,String str){ + System.out.println("你未能通过所有的测试用例!("+percent+"%)"); + System.out.println("你的输出为:"+str); + } + private boolean compare(ArrayList test,ArrayList standard){ + if(test.size()!=standard.size()){ + return false; + } + for(int i = 0;i