Skip to content

feat: add solutions to lc problem: No.0331 #1309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,112 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:栈**

我们将字符串 `preorder` 按逗号分割成数组,然后遍历数组,如果遇到了连续两个 `'#'`,并且第三个元素不是 `'#'`,那么就将这三个元素替换成一个 `'#'`,这个过程一直持续到数组遍历结束。

最后,判断数组长度是否为 $1$,且数组唯一的元素是否为 `'#'` 即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `preorder` 的长度。

<!-- tabs:start -->

### **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<String> 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<string> 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] == "#"
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<string> 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] == "#"
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
bool isValidSerialization(string preorder) {
vector<string> 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] == "#";
}
};
Original file line number Diff line number Diff line change
@@ -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] == "#"
}
Original file line number Diff line number Diff line change
@@ -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<String> 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("#");
}
}
Original file line number Diff line number Diff line change
@@ -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] == "#"
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 分别是矩阵的行数和列数。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
class Solution {
public:
int longestSquareStreak(vector<int>& nums) {
unordered_set<long long> 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<priority_queue<int>(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return priority_queue<int>();
}
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;
}
};
Loading