# LeetCode Kth Largest Element in an Array

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;
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：LeetCode Kth Largest Element in an Array 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)