Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
41 lines (40 loc) · 1.04 KB

File metadata and controls

41 lines (40 loc) · 1.04 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package leetcode;
public class leetcode215 {
public static void main(String[] args) {
new leetcode215().findKthLargest(new int[]{
3,2,3,1,2,4,5,5,6
},7);
}
public int findKthLargest(int[] nums, int k) {
if(k<1) return 0;
if (k==nums.length){
int min=nums[0];
for (int i = 0; i <nums.length ; i++) {
if (min>nums[i]) min=nums[i];
}
return min;
}
KwidthArrayMin(nums,k);
for(int i=k;i<nums.length;i++){
if(nums[i]>nums[0]){
swap(nums,0,i);
KwidthArrayMin(nums,k);
}
}
return nums[0];
}
public void KwidthArrayMin(int[] nums, int k) {
int min=nums[0];
for(int i=1;i<k;i++){
if(nums[i]<min){
swap(nums,0,i);
min=nums[0];
}
}
}
public void swap(int[] nums, int s,int e) {
int temp=nums[s];
nums[s]=nums[e];
nums[e]=temp;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.