diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3ab00d5..e96687a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,13 @@ - + + - + + + @@ -628,7 +638,6 @@ - @@ -653,7 +662,8 @@ - @@ -668,10 +678,10 @@ - + - + @@ -722,18 +732,26 @@ - + - + + + + + + + + + diff --git a/README.md b/README.md index 2119c07..a7c0f2a 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,14 @@ * [q54_螺旋矩阵](/src/数组操作/q54_螺旋矩阵) * [q73_矩阵置零](/src/数组操作/q73_矩阵置零) -### 栈操作 +### 栈相关 + +* [q20_有效的括号](/src/栈相关/q20_有效的括号) +* [q224_基本计算器](/src/栈相关/q224_基本计算器) + +### 堆相关 + -* [q20_有效的括号](/src/栈操作/q20_有效的括号) -* [q224_基本计算器](/src/栈操作/q224_基本计算器) ### 区间合并 diff --git a/src/堆相关/q215_数组中的第K个最大元素/Solution.java b/src/堆相关/q215_数组中的第K个最大元素/Solution.java new file mode 100644 index 0000000..89989e3 --- /dev/null +++ b/src/堆相关/q215_数组中的第K个最大元素/Solution.java @@ -0,0 +1,23 @@ +package 堆相关.q215_数组中的第K个最大元素; + +import java.util.PriorityQueue; + +/** + * 利用大根堆实现 o(n*log(k)) + */ +public class Solution { + + public int findKthLargest(int[] nums, int k) { + PriorityQueue heap = + new PriorityQueue<>((n1, n2) -> n1 - n2); + + for (int n: nums) { + heap.add(n); + if (heap.size() > k){ + heap.poll(); + } + } + + return heap.poll(); + } +} diff --git a/src/堆相关/q347_前K个高频元素/Solution.java b/src/堆相关/q347_前K个高频元素/Solution.java new file mode 100644 index 0000000..5b7b157 --- /dev/null +++ b/src/堆相关/q347_前K个高频元素/Solution.java @@ -0,0 +1,32 @@ +package 堆相关.q347_前K个高频元素; + +import java.util.*; + +/** + * 利用大根堆(PriorityQueue)实现 o(n*log(k)) + */ +class Solution { + public List topKFrequent(int[] nums, int k) { + + HashMap count = new HashMap<>(); + for (int n : nums) { + count.put(n, count.getOrDefault(n, 0) + 1); + } + + PriorityQueue heap = new PriorityQueue<>(Comparator.comparingInt(count::get)); + + for (int n : count.keySet()) { + heap.add(n); + if (heap.size() > k) { + heap.poll(); + } + } + + List topK = new LinkedList<>(); + while (!heap.isEmpty()) { + topK.add(heap.poll()); + } + Collections.reverse(topK); + return topK; + } +} diff --git a/src/栈操作/q20_有效的括号/Solution.java b/src/栈相关/q20_有效的括号/Solution.java similarity index 96% rename from src/栈操作/q20_有效的括号/Solution.java rename to src/栈相关/q20_有效的括号/Solution.java index 05d7528..ae64d8e 100644 --- a/src/栈操作/q20_有效的括号/Solution.java +++ b/src/栈相关/q20_有效的括号/Solution.java @@ -1,4 +1,4 @@ -package 栈操作.q20_有效的括号; +package 栈相关.q20_有效的括号; import java.util.Stack; diff --git a/src/栈操作/q224_基本计算器/f1/Solution.java b/src/栈相关/q224_基本计算器/f1/Solution.java similarity index 98% rename from src/栈操作/q224_基本计算器/f1/Solution.java rename to src/栈相关/q224_基本计算器/f1/Solution.java index cfded09..cdfad7c 100644 --- a/src/栈操作/q224_基本计算器/f1/Solution.java +++ b/src/栈相关/q224_基本计算器/f1/Solution.java @@ -1,4 +1,4 @@ -package 栈操作.q224_基本计算器.f1; +package 栈相关.q224_基本计算器.f1; import java.util.Stack; diff --git a/src/栈操作/q224_基本计算器/f2/Solution.java b/src/栈相关/q224_基本计算器/f2/Solution.java similarity index 97% rename from src/栈操作/q224_基本计算器/f2/Solution.java rename to src/栈相关/q224_基本计算器/f2/Solution.java index aec71cb..4dd667b 100644 --- a/src/栈操作/q224_基本计算器/f2/Solution.java +++ b/src/栈相关/q224_基本计算器/f2/Solution.java @@ -1,4 +1,4 @@ -package 栈操作.q224_基本计算器.f2; +package 栈相关.q224_基本计算器.f2; import java.util.Stack;