Skip to content

Commit 6635b3c

Browse files
committed
feat: add solutions to lc problem: No.1676
No.1676.Lowest Common Ancestor of a Binary Tree IV
1 parent bcf70ff commit 6635b3c

File tree

11 files changed

+132
-56
lines changed

11 files changed

+132
-56
lines changed

solution/1600-1699/1668.Maximum Repeating Substring/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050

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

53-
**方法一:暴力枚举**
53+
**方法一:直接枚举**
5454

55-
直接从大到小枚举 `word` 的重复次数,判断 `word` 重复该次数后是否是 `sequence` 的子串即可
55+
注意到字符串长度不超过 $100$,我们直接从大到小枚举 `word` 的重复次数 $k$,判断 `word` 重复该次数后是否是 `sequence` 的子串,是则直接返回当前的重复次数 $k$
5656

5757
时间复杂度为 $O(n^2)$,其中 $n$ 为 `sequence` 的长度。
5858

@@ -65,11 +65,9 @@
6565
```python
6666
class Solution:
6767
def maxRepeating(self, sequence: str, word: str) -> int:
68-
x = len(sequence) // len(word)
69-
for k in range(x, 0, -1):
68+
for k in range(len(sequence) // len(word), -1, -1):
7069
if word * k in sequence:
7170
return k
72-
return 0
7371
```
7472

7573
### **Java**
@@ -79,8 +77,7 @@ class Solution:
7977
```java
8078
class Solution {
8179
public int maxRepeating(String sequence, String word) {
82-
int x = sequence.length() / word.length();
83-
for (int k = x; k > 0; --k) {
80+
for (int k = sequence.length() / word.length(); k > 0; --k) {
8481
if (sequence.contains(word.repeat(k))) {
8582
return k;
8683
}
@@ -100,6 +97,7 @@ public:
10097
string t = word;
10198
int x = sequence.size() / word.size();
10299
for (int k = 1; k <= x; ++k) {
100+
// C++ 这里从小到大枚举重复值
103101
if (sequence.find(t) != string::npos) {
104102
ans = k;
105103
}
@@ -114,8 +112,7 @@ public:
114112
115113
```go
116114
func maxRepeating(sequence string, word string) int {
117-
x := len(sequence) / len(word)
118-
for k := x; k > 0; k-- {
115+
for k := len(sequence) / len(word); k > 0; k-- {
119116
if strings.Contains(sequence, strings.Repeat(word, k)) {
120117
return k
121118
}

solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,17 @@
5151
```python
5252
class Solution:
5353
def maxRepeating(self, sequence: str, word: str) -> int:
54-
x = len(sequence) // len(word)
55-
for k in range(x, 0, -1):
54+
for k in range(len(sequence) // len(word), -1, -1):
5655
if word * k in sequence:
5756
return k
58-
return 0
5957
```
6058

6159
### **Java**
6260

6361
```java
6462
class Solution {
6563
public int maxRepeating(String sequence, String word) {
66-
int x = sequence.length() / word.length();
67-
for (int k = x; k > 0; --k) {
64+
for (int k = sequence.length() / word.length(); k > 0; --k) {
6865
if (sequence.contains(word.repeat(k))) {
6966
return k;
7067
}
@@ -98,8 +95,7 @@ public:
9895
9996
```go
10097
func maxRepeating(sequence string, word string) int {
101-
x := len(sequence) / len(word)
102-
for k := x; k > 0; k-- {
98+
for k := len(sequence) / len(word); k > 0; k-- {
10399
if strings.Contains(sequence, strings.Repeat(word, k)) {
104100
return k
105101
}

solution/1600-1699/1668.Maximum Repeating Substring/Solution.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
func maxRepeating(sequence string, word string) int {
2-
x := len(sequence) / len(word)
3-
for k := x; k > 0; k-- {
2+
for k := len(sequence) / len(word); k > 0; k-- {
43
if strings.Contains(sequence, strings.Repeat(word, k)) {
54
return k
65
}

solution/1600-1699/1668.Maximum Repeating Substring/Solution.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution {
22
public int maxRepeating(String sequence, String word) {
3-
int x = sequence.length() / word.length();
4-
for (int k = x; k > 0; --k) {
3+
for (int k = sequence.length() / word.length(); k > 0; --k) {
54
if (sequence.contains(word.repeat(k))) {
65
return k;
76
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class Solution:
22
def maxRepeating(self, sequence: str, word: str) -> int:
3-
x = len(sequence) // len(word)
4-
for k in range(x, 0, -1):
3+
for k in range(len(sequence) // len(word), -1, -1):
54
if word * k in sequence:
65
return k
7-
return 0

solution/1600-1699/1675.Minimize Deviation in Array/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
<ul>
1414
<li>如果元素是<strong> 偶数</strong> ,<strong>除以</strong> <code>2</code>
15-
1615
<ul>
1716
<li>例如,如果数组是 <code>[1,2,3,4]</code> ,那么你可以对最后一个元素执行此操作,使其变成 <code>[1,2,3,<strong>2</strong>]</code></li>
1817
</ul>
@@ -22,7 +21,6 @@
2221
<li>例如,如果数组是 <code>[1,2,3,4]</code> ,那么你可以对第一个元素执行此操作,使其变成 <code>[<strong>2</strong>,2,3,4]</code></li>
2322
</ul>
2423
</li>
25-
2624
</ul>
2725

2826
<p>数组的 <strong>偏移量</strong> 是数组中任意两个元素之间的 <strong>最大差值</strong> 。</p>

solution/1600-1699/1675.Minimize Deviation in Array/README_EN.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.
13-
1413
<ul>
1514
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
1615
</ul>
@@ -20,7 +19,6 @@
2019
<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
2120
</ul>
2221
</li>
23-
2422
</ul>
2523

2624
<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,53 @@ class Solution {
147147
*/
148148
class Solution {
149149
public:
150-
unordered_set<int> s;
151-
152150
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
151+
unordered_set<int> s;
153152
for (auto node : nodes) s.insert(node->val);
153+
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
154+
if (!root || s.count(root->val)) return root;
155+
auto left = dfs(root->left);
156+
auto right = dfs(root->right);
157+
if (!left) return right;
158+
if (!right) return left;
159+
return root;
160+
};
154161
return dfs(root);
155162
}
163+
};
164+
```
156165
157-
TreeNode* dfs(TreeNode* root) {
158-
if (!root || s.count(root->val)) return root;
159-
auto left = dfs(root->left);
160-
auto right = dfs(root->right);
161-
if (!left) return right;
162-
if (!right) return left;
163-
return root;
166+
### **JavaScript**
167+
168+
```js
169+
/**
170+
* Definition for a binary tree node.
171+
* function TreeNode(val) {
172+
* this.val = val;
173+
* this.left = this.right = null;
174+
* }
175+
*/
176+
/**
177+
* @param {TreeNode} root
178+
* @param {TreeNode[]} nodes
179+
* @return {TreeNode}
180+
*/
181+
var lowestCommonAncestor = function (root, nodes) {
182+
const s = new Set();
183+
for (const node of nodes) {
184+
s.add(node.val);
185+
}
186+
function dfs(root) {
187+
if (!root || s.has(root.val)) {
188+
return root;
189+
}
190+
const [left, right] = [dfs(root.left), dfs(root.right)];
191+
if (left && right) {
192+
return root;
193+
}
194+
return left || right;
164195
}
196+
return dfs(root);
165197
};
166198
```
167199

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,53 @@ class Solution {
131131
*/
132132
class Solution {
133133
public:
134-
unordered_set<int> s;
135-
136134
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
135+
unordered_set<int> s;
137136
for (auto node : nodes) s.insert(node->val);
137+
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
138+
if (!root || s.count(root->val)) return root;
139+
auto left = dfs(root->left);
140+
auto right = dfs(root->right);
141+
if (!left) return right;
142+
if (!right) return left;
143+
return root;
144+
};
138145
return dfs(root);
139146
}
147+
};
148+
```
140149
141-
TreeNode* dfs(TreeNode* root) {
142-
if (!root || s.count(root->val)) return root;
143-
auto left = dfs(root->left);
144-
auto right = dfs(root->right);
145-
if (!left) return right;
146-
if (!right) return left;
147-
return root;
150+
### **JavaScript**
151+
152+
```js
153+
/**
154+
* Definition for a binary tree node.
155+
* function TreeNode(val) {
156+
* this.val = val;
157+
* this.left = this.right = null;
158+
* }
159+
*/
160+
/**
161+
* @param {TreeNode} root
162+
* @param {TreeNode[]} nodes
163+
* @return {TreeNode}
164+
*/
165+
var lowestCommonAncestor = function (root, nodes) {
166+
const s = new Set();
167+
for (const node of nodes) {
168+
s.add(node.val);
169+
}
170+
function dfs(root) {
171+
if (!root || s.has(root.val)) {
172+
return root;
173+
}
174+
const [left, right] = [dfs(root.left), dfs(root.right)];
175+
if (left && right) {
176+
return root;
177+
}
178+
return left || right;
148179
}
180+
return dfs(root);
149181
};
150182
```
151183

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@
1111
*/
1212
class Solution {
1313
public:
14-
unordered_set<int> s;
15-
1614
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
15+
unordered_set<int> s;
1716
for (auto node : nodes) s.insert(node->val);
17+
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
18+
if (!root || s.count(root->val)) return root;
19+
auto left = dfs(root->left);
20+
auto right = dfs(root->right);
21+
if (!left) return right;
22+
if (!right) return left;
23+
return root;
24+
};
1825
return dfs(root);
1926
}
20-
21-
TreeNode* dfs(TreeNode* root) {
22-
if (!root || s.count(root->val)) return root;
23-
auto left = dfs(root->left);
24-
auto right = dfs(root->right);
25-
if (!left) return right;
26-
if (!right) return left;
27-
return root;
28-
}
2927
};

0 commit comments

Comments
 (0)