215. Kth Largest Element in an Array找出数组中第k大的值

堆排序做的,没有全部排序,找到第k个就结束

public int findKthLargest(int[] nums, int k) {
        int num = 0;
        if (nums.length <= 1)
            return nums[0];
        int heapSize = nums.length;
        //1.构建最大堆
        int half = (heapSize-2)/2;
        for (int i = half;i >= 0;i--)
        {
            adjust(nums,heapSize,i);
        }
        while (heapSize > 1)
        {
            //2.取出最大值
            swap(0,heapSize-1,nums);
            heapSize--;
            num++;
            if(num == k)
                break;
            //3.调整堆
            adjust(nums,heapSize,0);
        }
        return nums[nums.length-k];
    }

    public void adjust(int[] nums,int heapSize,int index)
    {
        int left = index*2+1;
        int right = index*2+2;
        int biggest = index;
        if (left < heapSize && nums[left] > nums[biggest])
            biggest = left;
        //注意这里都是和nums[biggest]比较,别写成index,因为要找出最大的值
        if (right < heapSize && nums[right] > nums[biggest])
            biggest = right;
        if (biggest != index)
        {
            swap(index,biggest,nums);
            adjust(nums,heapSize,biggest);
        }
    }
    public void swap(int a,int b,int[] nums)
    {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
时间: 2024-10-06 01:21:58

215. Kth Largest Element in an Array找出数组中第k大的值的相关文章

【滴滴笔试】1.连续数组的最大和 2.找出数组中第K大的数

1 import java.util.Arrays; 2 import java.util.Scanner; 3 public class Main { 4 public static void main(String args[]){ 5 Scanner scanner = new Scanner(System.in); 6 while (scanner.hasNext()){ 7 String str = scanner.nextLine(); 8 String[] arrStr = str

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

刷题215. Kth Largest Element in an Array

一.题目说明 题目215. Kth Largest Element in an Array,在一个无序数组中找第k大的元素.难度是Medium! 二.我的解答 这个题目最直观的解答是,先对数组排序,然后直接返回: class Solution{ public: int findKthLargest(vector<int>& nums,int k){ sort(nums.begin(),nums.end()); return nums[nums.size()-k]; } }; 性能如下:

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

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

【LeetCode】215. Kth Largest Element in an Array (2 solutions)

Kth Largest Element in an Array 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

找一个数组中第K大的数

快排思想,选取序列的一个key进行划分,小于key的分在左边,大于key的在右边,key的位置-low+1就是最后一个元素是key的序列中元素的数量,当元素数量大于K时,就在左半部分递归找,等于时 arr[key]就是第K 大的元素,小于K时,在右边递归找第k-num大的元素 /** * 文件名:FindK.java * 时间:2014年11月7日上午11:23:43 * 作者:修维康 */ package chapter7; /** * 类名:FindK 说明:找到一个数组中第K大的元素 */

215. Kth Largest Element in an Array

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 le

LeetCode 215 : Kth Largest Element in an Array

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