diff --git a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README.md b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README.md index 08c48ad3a3ecd..12b7bafce306e 100644 --- a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README.md +++ b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README.md @@ -59,6 +59,14 @@ +**方法一:栈** + +我们将字符串 `preorder` 按逗号分割成数组,然后遍历数组,如果遇到了连续两个 `'#'`,并且第三个元素不是 `'#'`,那么就将这三个元素替换成一个 `'#'`,这个过程一直持续到数组遍历结束。 + +最后,判断数组长度是否为 $1$,且数组唯一的元素是否为 `'#'` 即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `preorder` 的长度。 + ### **Python3** @@ -66,7 +74,15 @@ ```python - +class Solution: + def isValidSerialization(self, preorder: str) -> bool: + stk = [] + for c in preorder.split(","): + stk.append(c) + while len(stk) > 2 and stk[-1] == stk[-2] == "#" and stk[-3] != "#": + stk = stk[:-3] + stk.append("#") + return len(stk) == 1 and stk[0] == "#" ``` ### **Java** @@ -74,7 +90,81 @@ ```java +class Solution { + public boolean isValidSerialization(String preorder) { + String[] strs = preorder.split(","); + int diff = 1; + for (String s : strs) { + if (--diff < 0) { + return false; + } + if (!s.equals("#")) { + diff += 2; + } + } + return diff == 0; + } +} +``` + +```java +class Solution { + public boolean isValidSerialization(String preorder) { + List stk = new ArrayList<>(); + for (String s : preorder.split(",")) { + stk.add(s); + while (stk.size() >= 3 + && stk.get(stk.size() - 1).equals("#") + && stk.get(stk.size() - 2).equals("#") + && !stk.get(stk.size() - 3).equals("#")) { + stk.remove(stk.size() - 1); + stk.remove(stk.size() - 1); + stk.remove(stk.size() - 1); + stk.add("#"); + } + } + return stk.size() == 1 && stk.get(0).equals("#"); + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool isValidSerialization(string preorder) { + vector stk; + stringstream ss(preorder); + string s; + while (getline(ss, s, ',')) { + stk.push_back(s); + while (stk.size() >= 3 && stk[stk.size() - 1] == "#" && stk[stk.size() - 2] == "#" && stk[stk.size() - 3] != "#") { + stk.pop_back(); + stk.pop_back(); + stk.pop_back(); + stk.push_back("#"); + } + } + return stk.size() == 1 && stk[0] == "#"; + } +}; +``` +### **Go** + +```go +func isValidSerialization(preorder string) bool { + stk := []string{} + for _, s := range strings.Split(preorder, ",") { + stk = append(stk, s) + for len(stk) >= 3 && stk[len(stk)-1] == "#" && stk[len(stk)-2] == "#" && stk[len(stk)-3] != "#" { + stk = stk[:len(stk)-3] + stk = append(stk, "#") + } + } + return len(stk) == 1 && stk[0] == "#" +} ``` ### **...** diff --git a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README_EN.md b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README_EN.md index 1111b7c6d88a5..a8ab5a476aa72 100644 --- a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README_EN.md +++ b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README_EN.md @@ -46,13 +46,95 @@ ### **Python3** ```python - +class Solution: + def isValidSerialization(self, preorder: str) -> bool: + stk = [] + for c in preorder.split(","): + stk.append(c) + while len(stk) > 2 and stk[-1] == stk[-2] == "#" and stk[-3] != "#": + stk = stk[:-3] + stk.append("#") + return len(stk) == 1 and stk[0] == "#" ``` ### **Java** ```java +class Solution { + public boolean isValidSerialization(String preorder) { + String[] strs = preorder.split(","); + int diff = 1; + for (String s : strs) { + if (--diff < 0) { + return false; + } + if (!s.equals("#")) { + diff += 2; + } + } + return diff == 0; + } +} +``` + +```java +class Solution { + public boolean isValidSerialization(String preorder) { + List stk = new ArrayList<>(); + for (String s : preorder.split(",")) { + stk.add(s); + while (stk.size() >= 3 + && stk.get(stk.size() - 1).equals("#") + && stk.get(stk.size() - 2).equals("#") + && !stk.get(stk.size() - 3).equals("#")) { + stk.remove(stk.size() - 1); + stk.remove(stk.size() - 1); + stk.remove(stk.size() - 1); + stk.add("#"); + } + } + return stk.size() == 1 && stk.get(0).equals("#"); + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool isValidSerialization(string preorder) { + vector stk; + stringstream ss(preorder); + string s; + while (getline(ss, s, ',')) { + stk.push_back(s); + while (stk.size() >= 3 && stk[stk.size() - 1] == "#" && stk[stk.size() - 2] == "#" && stk[stk.size() - 3] != "#") { + stk.pop_back(); + stk.pop_back(); + stk.pop_back(); + stk.push_back("#"); + } + } + return stk.size() == 1 && stk[0] == "#"; + } +}; +``` +### **Go** + +```go +func isValidSerialization(preorder string) bool { + stk := []string{} + for _, s := range strings.Split(preorder, ",") { + stk = append(stk, s) + for len(stk) >= 3 && stk[len(stk)-1] == "#" && stk[len(stk)-2] == "#" && stk[len(stk)-3] != "#" { + stk = stk[:len(stk)-3] + stk = append(stk, "#") + } + } + return len(stk) == 1 && stk[0] == "#" +} ``` ### **...** diff --git a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.cpp b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.cpp new file mode 100644 index 0000000000000..9ddd5721be207 --- /dev/null +++ b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + bool isValidSerialization(string preorder) { + vector stk; + stringstream ss(preorder); + string s; + while (getline(ss, s, ',')) { + stk.push_back(s); + while (stk.size() >= 3 && stk[stk.size() - 1] == "#" && stk[stk.size() - 2] == "#" && stk[stk.size() - 3] != "#") { + stk.pop_back(); + stk.pop_back(); + stk.pop_back(); + stk.push_back("#"); + } + } + return stk.size() == 1 && stk[0] == "#"; + } +}; \ No newline at end of file diff --git a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.go b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.go new file mode 100644 index 0000000000000..7ae0026d8e440 --- /dev/null +++ b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.go @@ -0,0 +1,11 @@ +func isValidSerialization(preorder string) bool { + stk := []string{} + for _, s := range strings.Split(preorder, ",") { + stk = append(stk, s) + for len(stk) >= 3 && stk[len(stk)-1] == "#" && stk[len(stk)-2] == "#" && stk[len(stk)-3] != "#" { + stk = stk[:len(stk)-3] + stk = append(stk, "#") + } + } + return len(stk) == 1 && stk[0] == "#" +} \ No newline at end of file diff --git a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.java b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.java index b276abe7ec469..d356d1e8b3478 100644 --- a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.java +++ b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.java @@ -1,15 +1,18 @@ -class Solution { - public boolean isValidSerialization(String preorder) { - String[] strs = preorder.split(","); - int diff = 1; - for (String s : strs) { - if (--diff < 0) { - return false; - } - if (!s.equals("#")) { - diff += 2; - } - } - return diff == 0; - } -} +class Solution { + public boolean isValidSerialization(String preorder) { + List stk = new ArrayList<>(); + for (String s : preorder.split(",")) { + stk.add(s); + while (stk.size() >= 3 + && stk.get(stk.size() - 1).equals("#") + && stk.get(stk.size() - 2).equals("#") + && !stk.get(stk.size() - 3).equals("#")) { + stk.remove(stk.size() - 1); + stk.remove(stk.size() - 1); + stk.remove(stk.size() - 1); + stk.add("#"); + } + } + return stk.size() == 1 && stk.get(0).equals("#"); + } +} \ No newline at end of file diff --git a/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.py b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.py new file mode 100644 index 0000000000000..957ae49481c8e --- /dev/null +++ b/solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def isValidSerialization(self, preorder: str) -> bool: + stk = [] + for c in preorder.split(","): + stk.append(c) + while len(stk) > 2 and stk[-1] == stk[-2] == "#" and stk[-3] != "#": + stk = stk[:-3] + stk.append("#") + return len(stk) == 1 and stk[0] == "#" diff --git a/solution/2500-2599/2500.Delete Greatest Value in Each Row/README.md b/solution/2500-2599/2500.Delete Greatest Value in Each Row/README.md index 69d54b769f55f..fb3b5cdba6086 100644 --- a/solution/2500-2599/2500.Delete Greatest Value in Each Row/README.md +++ b/solution/2500-2599/2500.Delete Greatest Value in Each Row/README.md @@ -66,9 +66,9 @@ 由于每一次操作都是从每一行中删除最大值,然后取最大值加到答案中,因此我们可以先对每一行进行排序。 -然后遍历每一列,取每一列的最大值,然后将其加到答案中即可。 +接下来遍历每一列,取每一列的最大值,然后将其加到答案中即可。 -时间复杂度 $O(m \times n \times \log n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 +时间复杂度 $O(m \times n \times \log n)$,空间复杂度 $O(\log n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.cpp b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.cpp index 619f5422d823d..b4019d704d053 100644 --- a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.cpp +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.cpp @@ -1,17 +1,41 @@ -class Solution { -public: - int longestSquareStreak(vector& nums) { - unordered_set s(nums.begin(), nums.end()); - int ans = -1; - for (int& v : nums) { - int t = 0; - long long x = v; - while (s.count(x)) { - x *= x; - ++t; - } - if (t > 1) ans = max(ans, t); - } - return ans; - } +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int countGreatEnoughNodes(TreeNode* root, int k) { + int ans = 0; + function(TreeNode*)> dfs = [&](TreeNode* root) { + if (!root) { + return priority_queue(); + } + auto left = dfs(root->left); + auto right = dfs(root->right); + while (right.size()) { + left.push(right.top()); + right.pop(); + if (left.size() > k) { + left.pop(); + } + } + if (left.size() == k && left.top() < root->val) { + ++ans; + } + left.push(root->val); + if (left.size() > k) { + left.pop(); + } + return left; + }; + dfs(root); + return ans; + } }; \ No newline at end of file diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go index f611bb5353c46..6588ed4a222d1 100644 --- a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go @@ -1,18 +1,46 @@ -func longestSquareStreak(nums []int) int { - s := map[int]bool{} - for _, v := range nums { - s[v] = true - } - ans := -1 - for _, v := range nums { - t := 0 - for s[v] { - v *= v - t++ +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func countGreatEnoughNodes(root *TreeNode, k int) (ans int) { + var dfs func(*TreeNode) hp + dfs = func(root *TreeNode) hp { + if root == nil { + return hp{} + } + l, r := dfs(root.Left), dfs(root.Right) + for _, x := range r.IntSlice { + l.push(x) + if l.Len() > k { + l.pop() + } + } + if l.Len() == k && root.Val > l.IntSlice[0] { + ans++ } - if t > 1 && t > ans { - ans = t + l.push(root.Val) + if l.Len() > k { + l.pop() } + return l } - return ans -} \ No newline at end of file + dfs(root) + return +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() interface{} { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} +func (h *hp) push(v int) { heap.Push(h, v) } +func (h *hp) pop() int { return heap.Pop(h).(int) } \ No newline at end of file