This commit is contained in:
fanxb 2021-08-29 23:14:21 +08:00
parent 5a2adb11cb
commit 3eb45138ef
2 changed files with 66 additions and 3 deletions

View File

@ -0,0 +1,44 @@
package com.fanxb.common;
import java.util.*;
public class Q215 {
public int findKthLargest(int[] nums, int k) {
//
List<Integer> heads = new ArrayList<>(k + 1);
heads.add(0);
for (int num : nums) {
//建堆
if (heads.size() < k + 1) {
heads.add(num);
for (int i = heads.size() - 1; i > 0; i--) {
cal(heads, i);
}
} else if (num >= heads.get(1)) {
heads.set(1, num);
cal(heads, 1);
}
}
return heads.get(1);
}
private static void cal(List<Integer> heads, int i) {
int left = 2 * i;
if (left < heads.size() && heads.get(i) > heads.get(left)) {
Collections.swap(heads, i, left);
cal(heads, left);
}
int right = 2 * i + 1;
if (right < heads.size() && heads.get(i) > heads.get(right)) {
Collections.swap(heads, i, right);
cal(heads, right);
}
}
public static void main(String[] args) {
int[] people = {3, 2, 3, 1, 2, 4, 5, 5, 6};
System.out.println(new Q215().findKthLargest(people, 4));
}
}

View File

@ -1,5 +1,7 @@
package com.fanxb.common;
import java.util.*;
/**
* Created with IntelliJ IDEA
*
@ -24,15 +26,32 @@ public class Q264 {
if (uglyArr[count - 1] < values[min]) {
//数字不重复才记录
uglyArr[count] = values[min];
count++;
;
}
uglyIndex[min]++;
values[min] = min == 0 ? 2 * uglyArr[uglyIndex[min]] : min == 1 ? 3 * uglyArr[uglyIndex[min]] : 5 * uglyArr[uglyIndex[min]] ;
values[min] = min == 0 ? 2 * uglyArr[uglyIndex[min]] : min == 1 ? 3 * uglyArr[uglyIndex[min]] : 5 * uglyArr[uglyIndex[min]];
}
return uglyArr[count - 1];
}
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
System.out.println(new Q264().nthUglyNumber(16));
// List<String> st<();
Map<String, String> map = new HashMap<>();
Set<String> set = new HashSet<>();
Runnable runnable = () -> {
try {
System.out.println("我在运行,开始等待");
// Object.wait();
System.out.println("睡觉完了");
} catch (Exception e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(2000);
Thread.currentThread().notify();
}
}