zoukankan      html  css  js  c++  java
  • leetcode 215. Kth Largest Element in an Array 寻找第k个大的数---------- java

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

    For example,
    Given [3,2,1,5,6,4] and k = 2, return 5.

    Note: 
    You may assume k is always valid, 1 ≤ k ≤ array's length.

    寻找一个数组中第k大的数。

    1、建立一个大小为k的数组,每次维护这个数组,属于暴力做法。

    public class Solution {
        public int findKthLargest(int[] nums, int k) {
            int len = nums.length;
            int[] result = new int[k];
            for (int i = 0; i < k ; i++){
                result[i] = nums[i];
            }
            for (int i = k; i < len; i++){
                int min = result[0];
                int pos = 0;
                for (int j = 1; j < k; j++){
                    if (min > result[j]){
                        min = result[j];
                        pos = j;
                    }
                }
                if (nums[i] > min){
                    result[pos] = nums[i];
                }
            }
            int ans = result[0];
            for (int i = 1; i < k; i++){
                System.out.println(result[i] + " " + result[0]);
                ans = Math.min(ans, result[i]);
            }
            return ans;
        }
    }

    2、利用快排

    public class Solution {
        public int findKthLargest(int[] nums, int k) {
            return QuickSort(nums, k, 0, nums.length - 1);
        }
        
        public int QuickSort(int[] nums, int k, int start, int end){
            int left = start, right = end;
            int num = nums[start];
            for (int i = end; i > start; i--){
                if (nums[i] >= num){
                    swap(nums, end--, i);
                }
            }
            swap(nums, end, start);
            if (k == nums.length - end){
                return nums[end];
            } else if (k > nums.length - end){
                return QuickSort(nums, k, left, end - 1);
            } else {
                return QuickSort(nums, k, end + 1, right);
            }
        }
        public void swap(int[] nums, int a, int b){
            int num = nums[a];
            nums[a] = nums[b];
            nums[b] = num;
        }
    }

    3、discuss中的快排。速度更快

    public class Solution {
        
        public int findKthLargest(int[] nums, int k) {
    
            return select(nums, k-1);
        }
        
        // Quick select
        private int select(int[] nums, int k) {
            int left = 0, right = nums.length-1;
            while(true) {
                if(left == right)
                    return nums[left];
                int pivotIndex = medianOf3(nums, left, right);
                pivotIndex = partition(nums, left, right, pivotIndex);
                if(pivotIndex == k)
                    return nums[k];
                else if(pivotIndex > k)
                    right = pivotIndex-1;
                else
                    left = pivotIndex+1;
            }
        }
        
        //Use median-of-three strategy to choose pivot
        private int medianOf3(int[] nums, int left, int right) {
            int mid = left + (right - left) / 2;
            if(nums[right] > nums[left])
                swap(nums, left, right);
            if(nums[right] > nums[mid])
                swap(nums, right, mid);
            if(nums[mid] > nums[left])
                swap(nums,left, mid);
            return mid;
        }
        
        private int partition(int[] nums, int left, int right, int pivotIndex) {
            int pivotValue = nums[pivotIndex];
            swap(nums, pivotIndex, right);
            int index = left;
            for(int i = left; i < right; ++i) {
                if(nums[i] > pivotValue) {
                    swap(nums, index, i);
                    ++index;
                }
            }
            swap(nums, right, index);
            return index;
        }
        
        private void swap(int[] nums, int a, int b) {
            int temp = nums[a];
            nums[a] = nums[b];
            nums[b] = temp;
        }
    
    }
  • 相关阅读:
    Coursera课程笔记----计算导论与C语言基础----Week 7
    Coursera课程笔记----计算导论与C语言基础----Week 6
    Coursera课程笔记----计算导论与C语言基础----Week 5
    Coursera课程笔记----P4E.Capstone----Week 6&7
    Coursera课程笔记----P4E.Capstone----Week 4&5
    Coursera课程笔记----P4E.Capstone----Week 2&3
    图解 Java 垃圾回收机制,写得非常好!
    别在 Java 代码里乱打日志了,这才是正确的打日志姿势!
    聊一聊Java 泛型中的通配符 T,E,K,V,?
    Java开发最常犯的10个错误,打死都不要犯!
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6646134.html
Copyright © 2011-2022 走看看