add
This commit is contained in:
parent
5a2adb11cb
commit
3eb45138ef
44
5.leetcode/src/com/fanxb/common/Q215.java
Normal file
44
5.leetcode/src/com/fanxb/common/Q215.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package com.fanxb.common;
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created with IntelliJ IDEA
|
* Created with IntelliJ IDEA
|
||||||
*
|
*
|
||||||
@ -24,7 +26,7 @@ public class Q264 {
|
|||||||
if (uglyArr[count - 1] < values[min]) {
|
if (uglyArr[count - 1] < values[min]) {
|
||||||
//数字不重复才记录
|
//数字不重复才记录
|
||||||
uglyArr[count] = values[min];
|
uglyArr[count] = values[min];
|
||||||
count++;
|
;
|
||||||
}
|
}
|
||||||
uglyIndex[min]++;
|
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]];
|
||||||
@ -32,7 +34,24 @@ public class Q264 {
|
|||||||
return uglyArr[count - 1];
|
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));
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user