java---最小的K個數

來源:互聯網
上載者:User

標籤:簡單   offer   for   題目   最小   compareto   style   else   wrap   

時間限制:1秒 空間限制:32768K 題目描述輸入n個整數,找出其中最小的K個數。例如輸入4,5,1,6,2,7,3,8這8個數字,則最小的4個數字是1,2,3,4,。最簡單的思路當然是直接排序,最前面的K個數就是題目所求,但是這顯然不是優秀的解法。可以選擇使用快排的Partition函數,簡單劃分,但是並不排序,較為快速的解決問題。
import java.util.ArrayList;public class Solution {    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {        ArrayList<Integer> result=new ArrayList<Integer>();        if(k<=0){            return result;        }        if(input.length==0||input.length<k){            return result;        }        int start=0;        int end=input.length-1;        int index=Partition(input,start,end);        while(index!=k-1){            if(index>k-1){                end=index-1;                index=Partition(input,start,end);            }else{                start=index+1;                index=Partition(input,start,end);            }        }        for(int i=0;i<k;i++){            result.add(input[i]);        }        return result;    }    public int Partition(int[] input,int start,int end){        int pivot=input[start];        while(start<end){            while(start<end&&input[end]>=pivot){--end;}                input[start]=input[end];            while(start<end&&input[start]<=pivot){++start;}                input[end]=input[start];        }        input[end]=pivot;        return end;    }}

還可以考慮使用另一種資料結構,最大堆,用最大堆儲存這k個數,每次只和堆頂比,如果比堆頂小,刪除堆頂,新數入堆。

import java.util.ArrayList;import java.util.PriorityQueue;import java.util.Comparator;public class Solution {   public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {       ArrayList<Integer> result = new ArrayList<Integer>();       int length = input.length;       if(k > length || k == 0){           return result;       }        PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>() {             @Override            public int compare(Integer o1, Integer o2) {                return o2.compareTo(o1);            }        });        for (int i = 0; i < length; i++) {            if (maxHeap.size() != k) {                maxHeap.offer(input[i]);            } else if (maxHeap.peek() > input[i]) {                Integer temp = maxHeap.poll();                temp = null;                maxHeap.offer(input[i]);            }        }        for (Integer integer : maxHeap) {            result.add(integer);        }        return result;    }}

 

java---最小的K個數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.