0
点赞
收藏
分享

微信扫一扫

【LeeCode】215. 数组中的第K个最大元素

【题目描述】

给定整数数组 ​​nums​​​ 和整数 ​​k​​​,请返回数组中第 ​k​ 个最大的元素。

请注意,你需要找的是数组排序后的第 ​​k​​ 个最大的元素,而不是第 ​​k​​ 个不同的元素。

你必须设计并实现时间复杂度为 ​​O(n)​​ 的算法解决此问题。

 ​​https://leetcode.cn/problems/kth-largest-element-in-an-array/description/?favorite=2cktkvj​​


【示例】

 【LeeCode】215. 数组中的第K个最大元素_java


【代码】admin

package com.company;

import java.util.*;

// 2022-02-07
class Solution {
public int findKthLargest(int[] nums, int k) {
int len = nums.length;
Arrays.sort(nums);
System.out.println(nums[len - k]);
return -1;
}
}

public class Test {
public static void main(String[] args) {
new Solution().findKthLargest(new int[]{3,2,1,5,6,4}, 2); // 输出: 5
new Solution().findKthLargest(new int[]{3,2,3,1,2,4,5,5,6}, 4); // 输出: 4
}
}

举报

相关推荐

0 条评论