diff --git a/5.leetcode/src/com/fanxb/common/Q215.java b/5.leetcode/src/com/fanxb/common/Q215.java new file mode 100644 index 0000000..cc8b440 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q215.java @@ -0,0 +1,44 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q215 { + + public int findKthLargest(int[] nums, int k) { + //堆 + List 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 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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q264.java b/5.leetcode/src/com/fanxb/common/Q264.java index f7ecaf5..0785686 100644 --- a/5.leetcode/src/com/fanxb/common/Q264.java +++ b/5.leetcode/src/com/fanxb/common/Q264.java @@ -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 st<(); + Map map = new HashMap<>(); + Set 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(); } }