# LeetCode Kth Largest Element in an Array

130人阅读 评论(0)

Description:

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Solution:

import java.util.*;

public class Solution {

public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}
}

import java.util.*;

public class Solution {

public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}

int findKth(int nums[], int start, int end, int k) {
int mark = partition(nums, start, end);
if (mark == k)
return nums[mark];
else if (mark < k)
return findKth(nums, mark + 1, end, k - mark);
else
return findKth(nums, start, mark, k);

}

int partition(int[] nums, int start, int end) {
int mark = nums[start];
int tot = start + 1;

for (int i = start + 1; i <= end; i++) {
if (nums[i] <= mark) {
swap(nums, tot++, i);
}
}

swap(nums, start, tot);
}

void swap(int[] nums, int i, int j) {
int c = nums[i];
nums[i] = nums[j];
nums[j] = c;
}
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：62648次
• 积分：3261
• 等级：
• 排名：第10459名
• 原创：288篇
• 转载：0篇
• 译文：0篇
• 评论：4条
文章分类
阅读排行
最新评论