Skip to content

Commit 5bf6320

Browse files
committed
fd
1 parent 1f5e847 commit 5bf6320

File tree

5 files changed

+21
-28
lines changed

5 files changed

+21
-28
lines changed

solution/src/main/java/com/inuker/solution/KthLargestElementInArray.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class KthLargestElementInArray {
1212

1313
// 耗时15ms,时间复杂度O(nlgk),空间复杂度O(k)
1414
// 按升序排的,出队列k次获取第k大的数
15-
public int findKthLargest(int[] nums, int k) {
15+
public int findKthLargest2(int[] nums, int k) {
1616
PriorityQueue<Integer> queue = new PriorityQueue<>();
1717
for (int n : nums) {
1818
queue.offer(n);
@@ -34,30 +34,34 @@ public int findKthLargest3(int[] nums, int k) {
3434
// 对比快速排序T(n) = 2T(n / 2) + n = O(nlgn)
3535
// 区别在于这个被pivot分隔后,只用处理其中的一半,而快排两边都要处理
3636
// 耗时18ms
37-
public int findKthLargest4(int[] nums, int k) {
38-
return nums[findKthLargest4(nums, 0, nums.length - 1, k)];
37+
public int findKthLargest(int[] nums, int k) {
38+
return findKthLargest(nums, 0, nums.length - 1, k);
3939
}
4040

41-
public int findKthLargest4(int[] nums, int start, int end, int k) {
41+
public int findKthLargest(int[] nums, int start, int end, int k) {
4242
int pivot = partition(nums, start, end);
4343

4444
int rank = end - pivot + 1;
4545

4646
if (rank == k) {
47-
return pivot;
47+
return nums[pivot];
4848
} else if (rank > k) {
49-
return findKthLargest4(nums, pivot + 1, end, k);
49+
return findKthLargest(nums, pivot + 1, end, k);
5050
} else {
51-
return findKthLargest4(nums, start, pivot - 1, k - rank);
51+
return findKthLargest(nums, start, pivot - 1, k - rank);
5252
}
5353
}
5454

5555
public int partition(int[] nums, int start, int end) {
56-
int pivot = nums[end], left = start;
56+
int pivot = nums[end], left = start, right = end - 1;
5757

58-
for (int i = start; i < end; i++) {
59-
if (nums[i] <= pivot) {
60-
swap(nums, left++, i);
58+
for (int i = start; i <= right; ) {
59+
if (nums[i] < pivot) {
60+
swap(nums, left++, i++);
61+
} else if (nums[i] > pivot) {
62+
swap(nums, right--, i);
63+
} else {
64+
i++;
6165
}
6266
}
6367

solution/src/main/java/com/inuker/solution/MedianOfTwoSortedArrays.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* Created by dingjikerbo on 16/12/20.
5+
* https://leetcode.com/articles/median-of-two-sorted-arrays/
56
*/
67

78
public class MedianOfTwoSortedArrays {

solution/src/main/java/com/inuker/solution/MergeKSortedList.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* Created by dingjikerbo on 16/11/20.
11+
* https://leetcode.com/articles/merge-k-sorted-list/
1112
*/
1213

1314
public class MergeKSortedList {
@@ -23,13 +24,7 @@ public ListNode mergeKLists(ListNode[] lists) {
2324
PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
2425
@Override
2526
public int compare(ListNode node1, ListNode node2) {
26-
if (node1.val == node2.val) {
27-
return 0;
28-
} else if (node1.val < node2.val) {
29-
return -1;
30-
} else {
31-
return 1;
32-
}
27+
return node1.val - node2.val;
3328
}
3429
});
3530

solution/src/main/java/com/inuker/solution/WiggleSortII.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void wiggleSort2(int[] nums) {
3434
* 只要这样交叉放着就能符合wiggle
3535
* 直观的做法是另外开辟一个空间来放wiggle结果
3636
* 如果要求不开辟空间,则只能用坐标映射了
37+
* 类似的可以参考sort colors和find kth largest element
3738
*/
3839
// 时间复杂度O(n),空间复杂度O(l)
3940
public void wiggleSort(int[] nums) {

test/src/main/java/com/inuker/test/main.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,10 @@ public static void main(String[] args) {
2626
int[] arr = new int[] {
2727
6, 13, 5, 4, 5, 2
2828
};
29-
new WiggleSortII().wiggleSort(arr);
29+
int ff = new fd().findKthLargest(arr, 6);
30+
System.out.println(ff);
3031
for (int n : arr) {
3132
System.out.print(n + " ");
3233
}
3334
}
34-
35-
public void wiggleSort2(int[] nums) {
36-
int[] arr = nums.clone();
37-
Arrays.sort(arr);
38-
int n = nums.length, j = (n - 1) / 2, k = n - 1;
39-
for (int i = 0; i < nums.length; i++) {
40-
nums[i] = i % 2 == 0 ? arr[j--] : arr[k--];
41-
}
42-
}
4335
}

0 commit comments

Comments
 (0)