zoukankan      html  css  js  c++  java
  • 30:最小的K个数

    import java.util.ArrayList;
    import java.util.TreeSet;
    /**
     * 面试题30:最小的K个数
     * 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
     */
    public class _30_min_k_num {
    	public static void main(String[] args) {
    		int[] num={4,5,1,6,2,7,3,8};
    		Solution30 solution30 = new Solution30();
    		ArrayList<Integer> arrayList=solution30.GetLeastNumbers_Solution(num, 1);
    		for(Integer out:arrayList){
    			System.out.print(out+"、");
    		}
    	}
    }
    class Solution30 {
        public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        	ArrayList<Integer> arrayList = new ArrayList<Integer>();
        	if(input.length==0||k==0||k>input.length){
        		return arrayList;
        	}
        	TreeSet<Integer> treeSet = new TreeSet<Integer>();
        	for(int i=0;i<k;i++){
        		treeSet.add(input[i]);
        	}
        	for(int i=k;i<input.length;i++){
        		int tmpMax=treeSet.last();
        		if(input[i]<tmpMax){
        			treeSet.remove(treeSet.last());
        			treeSet.add(input[i]);
        		}
        	}
        	for(Integer s:treeSet){
        		arrayList.add(s);
        	}
        	return arrayList;
        }
    }
    
  • 相关阅读:
    自动化CodeReview
    10个有关RESTful API良好设计的最佳实践
    ASP.NET Core 获取控制器上的自定义属性
    [转] Autofac创建实例的方法总结
    PetaPoco
    LogViewer
    hdoj:2047
    hdoj:2046
    hdoj:2045
    hdoj:2044
  • 原文地址:https://www.cnblogs.com/andy-zhou/p/6549753.html
Copyright © 2011-2022 走看看