leet-code/src/main/java/leetcode/editor/cn/KthLargestElementInAnArray.java

52 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定整数数组 nums 和整数 k请返回数组中第 k 个最大的元素。
//
// 请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
//
//
//
// 示例 1:
//
//
//输入: [3,2,1,5,6,4] 和 k = 2
//输出: 5
//
//
// 示例 2:
//
//
//输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
//输出: 4
//
//
//
// 提示:
//
//
// 1 <= k <= nums.length <= 104
// -104 <= nums[i] <= 104
//
// Related Topics 数组 分治 快速选择 排序 堆(优先队列)
// 👍 1170 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.Queue;
//215:数组中的第K个最大元素
public class KthLargestElementInAnArray{
public static void main(String[] args) {
//测试代码
Solution solution = new KthLargestElementInAnArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}