Skip to content

Commit db7d6e6

Browse files
committed
feat: update lc problems
1 parent 8daacfe commit db7d6e6

File tree

69 files changed

+189
-155
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+189
-155
lines changed

solution/0000-0099/0097.Interleaving String/README_EN.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<p>Given strings <code>s1</code>, <code>s2</code>, and <code>s3</code>, find whether <code>s3</code> is formed by an <strong>interleaving</strong> of <code>s1</code> and <code>s2</code>.</p>
88

9-
<p>An <strong>interleaving</strong> of two strings <code>s</code> and <code>t</code> is a configuration where they are divided into <strong>non-empty</strong> substrings such that:</p>
9+
<p>An <strong>interleaving</strong> of two strings <code>s</code> and <code>t</code> is a configuration where <code>s</code> and <code>t</code> are divided into <code>n</code> and <code>m</code> <strong>non-empty</strong> substrings respectively, such that:</p>
1010

1111
<ul>
1212
<li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li>
@@ -23,13 +23,18 @@
2323
<pre>
2424
<strong>Input:</strong> s1 = &quot;aabcc&quot;, s2 = &quot;dbbca&quot;, s3 = &quot;aadbbcbcac&quot;
2525
<strong>Output:</strong> true
26+
<strong>Explanation:</strong> One way to obtain s3 is:
27+
Split s1 into s1 = &quot;aa&quot; + &quot;bc&quot; + &quot;c&quot;, and s2 into s2 = &quot;dbbc&quot; + &quot;a&quot;.
28+
Interleaving the two splits, we get &quot;aa&quot; + &quot;dbbc&quot; + &quot;bc&quot; + &quot;a&quot; + &quot;c&quot; = &quot;aadbbcbcac&quot;.
29+
Since s3 can be obtained by interleaving s1 and s2, we return true.
2630
</pre>
2731

2832
<p><strong>Example 2:</strong></p>
2933

3034
<pre>
3135
<strong>Input:</strong> s1 = &quot;aabcc&quot;, s2 = &quot;dbbca&quot;, s3 = &quot;aadbbbaccc&quot;
3236
<strong>Output:</strong> false
37+
<strong>Explanation:</strong> Notice how it is impossible to interleave s2 with any other string to obtain s3.
3338
</pre>
3439

3540
<p><strong>Example 3:</strong></p>

solution/0100-0199/0130.Surrounded Regions/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
<pre>
1515
<strong>Input:</strong> board = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]
1616
<strong>Output:</strong> [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]
17-
<strong>Explanation:</strong> Surrounded regions should not be on the border, which means that any &#39;O&#39; on the border of the board are not flipped to &#39;X&#39;. Any &#39;O&#39; that is not on the border and it is not connected to an &#39;O&#39; on the border will be flipped to &#39;X&#39;. Two cells are connected if they are adjacent cells connected horizontally or vertically.
17+
<strong>Explanation:</strong> Notice that an &#39;O&#39; should not be flipped if:
18+
- It is on the border, or
19+
- It is adjacent to an &#39;O&#39; that should not be flipped.
20+
The bottom &#39;O&#39; is on the border, so it is not flipped.
21+
The other three &#39;O&#39; form a surrounded region, so they are flipped.
1822
</pre>
1923

2024
<p><strong>Example 2:</strong></p>

solution/0100-0199/0139.Word Break/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Trie:
9090
def __init__(self):
9191
self.children = [None] * 26
9292
self.is_end = False
93-
93+
9494
def insert(self, w):
9595
node = self
9696
for c in w:
@@ -99,7 +99,7 @@ class Trie:
9999
node.children[idx] = Trie()
100100
node = node.children[idx]
101101
node.is_end = True
102-
102+
103103
def search(self, w):
104104
node = self
105105
for c in w:
@@ -114,7 +114,7 @@ class Solution:
114114
@cache
115115
def dfs(s):
116116
return not s or any(trie.search(s[:i]) and dfs(s[i:]) for i in range(1, len(s) + 1))
117-
117+
118118
trie = Trie()
119119
for w in wordDict:
120120
trie.insert(w)
@@ -269,7 +269,7 @@ public:
269269
270270
bool wordBreak(string s, vector<string>& wordDict) {
271271
for (auto w : wordDict) trie->insert(w);
272-
return dfs(s);
272+
return dfs(s);
273273
}
274274
275275
bool dfs(string s) {

solution/0100-0199/0139.Word Break/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Trie:
7272
def __init__(self):
7373
self.children = [None] * 26
7474
self.is_end = False
75-
75+
7676
def insert(self, w):
7777
node = self
7878
for c in w:
@@ -81,7 +81,7 @@ class Trie:
8181
node.children[idx] = Trie()
8282
node = node.children[idx]
8383
node.is_end = True
84-
84+
8585
def search(self, w):
8686
node = self
8787
for c in w:
@@ -96,7 +96,7 @@ class Solution:
9696
@cache
9797
def dfs(s):
9898
return not s or any(trie.search(s[:i]) and dfs(s[i:]) for i in range(1, len(s) + 1))
99-
99+
100100
trie = Trie()
101101
for w in wordDict:
102102
trie.insert(w)
@@ -249,7 +249,7 @@ public:
249249
250250
bool wordBreak(string s, vector<string>& wordDict) {
251251
for (auto w : wordDict) trie->insert(w);
252-
return dfs(s);
252+
return dfs(s);
253253
}
254254
255255
bool dfs(string s) {

solution/0200-0299/0212.Word Search II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public:
208208
if(node->w != "") res.insert(node->w);
209209
char c = board[i][j];
210210
board[i][j] = '0';
211-
211+
212212
for (int k = 0; k < 4; ++k)
213213
{
214214
int x = i + dirs[k], y = j + dirs[k + 1];

solution/0200-0299/0212.Word Search II/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public:
198198
if(node->w != "") res.insert(node->w);
199199
char c = board[i][j];
200200
board[i][j] = '0';
201-
201+
202202
for (int k = 0; k < 4; ++k)
203203
{
204204
int x = i + dirs[k], y = j + dirs[k + 1];

solution/0200-0299/0215.Kth Largest Element in an Array/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@
1010

1111
<p>请注意,你需要找的是数组排序后的第 <code>k</code> 个最大的元素,而不是第 <code>k</code> 个不同的元素。</p>
1212

13-
<p> </p>
13+
<p>&nbsp;</p>
1414

1515
<p><strong>示例 1:</strong></p>
1616

1717
<pre>
18-
<strong>输入:</strong> <code>[3,2,1,5,6,4]</code> k = 2
18+
<strong>输入:</strong> <code>[3,2,1,5,6,4],</code> k = 2
1919
<strong>输出:</strong> 5
2020
</pre>
2121

22-
<p><strong>示例 2:</strong></p>
22+
<p><strong>示例&nbsp;2:</strong></p>
2323

2424
<pre>
25-
<strong>输入:</strong> <code>[3,2,3,1,2,4,5,5,6]</code> k = 4
25+
<strong>输入:</strong> <code>[3,2,3,1,2,4,5,5,6], </code>k = 4
2626
<strong>输出:</strong> 4</pre>
2727

28-
<p> </p>
28+
<p>&nbsp;</p>
2929

3030
<p><strong>提示: </strong></p>
3131

3232
<ul>
33-
<li><code>1 <= k <= nums.length <= 10<sup>4</sup></code></li>
34-
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
33+
<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
34+
<li><code>-10<sup>4</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
3535
</ul>
3636

3737
## 解法

solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p>
1010

11+
<p>You must solve it in <code>O(n)</code> time complexity.</p>
12+
1113
<p>&nbsp;</p>
1214
<p><strong>Example 1:</strong></p>
1315
<pre><strong>Input:</strong> nums = [3,2,1,5,6,4], k = 2
@@ -20,7 +22,7 @@
2022
<p><strong>Constraints:</strong></p>
2123

2224
<ul>
23-
<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
25+
<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
2426
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
2527
</ul>
2628

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
## Description
66

7-
<p>Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.</p>
7+
<p>Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.</p>
88

9-
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).&rdquo;</p>
9+
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <strong>a node to be a descendant of itself</strong>).&rdquo;</p>
1010

1111
<p>&nbsp;</p>
1212
<p><strong>Example 1:</strong></p>

solution/0200-0299/0243.Shortest Word Distance/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<p><strong>Constraints:</strong></p>
2626

2727
<ul>
28-
<li><code>1 &lt;= wordsDict.length &lt;= 3 * 10<sup>4</sup></code></li>
28+
<li><code>2 &lt;= wordsDict.length &lt;= 3 * 10<sup>4</sup></code></li>
2929
<li><code>1 &lt;= wordsDict[i].length &lt;= 10</code></li>
3030
<li><code>wordsDict[i]</code> consists of lowercase English letters.</li>
3131
<li><code>word1</code> and <code>word2</code> are in <code>wordsDict</code>.</li>

solution/0200-0299/0290.Word Pattern/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88

99
<p>给定一种规律 <code>pattern</code>&nbsp;和一个字符串&nbsp;<code>s</code>&nbsp;,判断 <code>s</code>&nbsp;是否遵循相同的规律。</p>
1010

11-
<p>这里的&nbsp;<strong>遵循&nbsp;</strong>指完全匹配,例如,&nbsp;<code>pattern</code>&nbsp;里的每个字母和字符串&nbsp;<code>str</code><strong>&nbsp;</strong>中的每个非空单词之间存在着双向连接的对应规律。</p>
11+
<p>这里的&nbsp;<strong>遵循&nbsp;</strong>指完全匹配,例如,&nbsp;<code>pattern</code>&nbsp;里的每个字母和字符串&nbsp;<code>s</code><strong>&nbsp;</strong>中的每个非空单词之间存在着双向连接的对应规律。</p>
1212

1313
<p>&nbsp;</p>
1414

1515
<p><strong>示例1:</strong></p>
1616

1717
<pre>
18-
<strong>输入:</strong> pattern = <code>"abba"</code>, str = <code>"dog cat cat dog"</code>
18+
<strong>输入:</strong> pattern = <code>"abba"</code>, s = <code>"dog cat cat dog"</code>
1919
<strong>输出:</strong> true</pre>
2020

2121
<p><strong>示例 2:</strong></p>
2222

2323
<pre>
24-
<strong>输入:</strong>pattern = <code>"abba"</code>, str = <code>"dog cat cat fish"</code>
24+
<strong>输入:</strong>pattern = <code>"abba"</code>, s = <code>"dog cat cat fish"</code>
2525
<strong>输出:</strong> false</pre>
2626

2727
<p><strong>示例 3:</strong></p>
2828

2929
<pre>
30-
<strong>输入:</strong> pattern = <code>"aaaa"</code>, str = <code>"dog cat cat dog"</code>
30+
<strong>输入:</strong> pattern = <code>"aaaa"</code>, s = <code>"dog cat cat dog"</code>
3131
<strong>输出:</strong> false</pre>
3232

3333
<p>&nbsp;</p>

solution/0300-0399/0346.Moving Average from Data Stream/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class MovingAverage {
113113
public MovingAverage(int size) {
114114
arr = new int[size];
115115
}
116-
116+
117117
public double next(int val) {
118118
int idx = cnt % arr.length;
119119
s += val - arr[idx];
@@ -139,7 +139,7 @@ class MovingAverage {
139139
public MovingAverage(int size) {
140140
n = size;
141141
}
142-
142+
143143
public double next(int val) {
144144
if (q.size() == n) {
145145
s -= q.pollFirst();
@@ -165,7 +165,7 @@ public:
165165
MovingAverage(int size) {
166166
arr.resize(size);
167167
}
168-
168+
169169
double next(int val) {
170170
int idx = cnt % arr.size();
171171
s += val - arr[idx];
@@ -193,7 +193,7 @@ public:
193193
MovingAverage(int size) {
194194
n = size;
195195
}
196-
196+
197197
double next(int val) {
198198
if (q.size() == n)
199199
{

solution/0300-0399/0346.Moving Average from Data Stream/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class MovingAverage {
9999
public MovingAverage(int size) {
100100
arr = new int[size];
101101
}
102-
102+
103103
public double next(int val) {
104104
int idx = cnt % arr.length;
105105
s += val - arr[idx];
@@ -125,7 +125,7 @@ class MovingAverage {
125125
public MovingAverage(int size) {
126126
n = size;
127127
}
128-
128+
129129
public double next(int val) {
130130
if (q.size() == n) {
131131
s -= q.pollFirst();
@@ -151,7 +151,7 @@ public:
151151
MovingAverage(int size) {
152152
arr.resize(size);
153153
}
154-
154+
155155
double next(int val) {
156156
int idx = cnt % arr.size();
157157
s += val - arr[idx];
@@ -179,7 +179,7 @@ public:
179179
MovingAverage(int size) {
180180
n = size;
181181
}
182-
182+
183183
double next(int val) {
184184
if (q.size() == n)
185185
{

solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给你一个整数数组 <code>nums</code> ,返回<em> </em><code>nums[i] XOR nums[j]</code> 的最大运算结果,其中 <code>0 ≤ i ≤ j < n</code> 。</p>
9+
<p>给你一个整数数组 <code>nums</code> ,返回<em> </em><code>nums[i] XOR nums[j]</code> 的最大运算结果,其中 <code>0 ≤ i ≤ j &lt; n</code> 。</p>
1010

1111
<p><strong>进阶:</strong>你可以在 <code>O(n)</code> 的时间解决这个问题吗?</p>
1212

13-
<p> </p>
13+
<p>&nbsp;</p>
1414

1515
<div class="original__bRMd">
1616
<div>
@@ -49,13 +49,13 @@
4949
<strong>输出:</strong>127
5050
</pre>
5151

52-
<p> </p>
52+
<p>&nbsp;</p>
5353

5454
<p><strong>提示:</strong></p>
5555

5656
<ul>
57-
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
58-
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
57+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
58+
<li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
5959
</ul>
6060
</div>
6161
</div>

solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ class Solution:
5959
class Trie:
6060
def __init__(self):
6161
self.children = [None] * 2
62-
62+
6363
def insert(self, x):
6464
node = self
6565
for i in range(30, -1, -1):
6666
v = (x >> i) & 1
6767
if node.children[v] is None:
6868
node.children[v] = Trie()
6969
node = node.children[v]
70-
70+
7171
def search(self, x):
7272
node = self
7373
res = 0

solution/0400-0499/0472.Concatenated Words/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public:
193193

194194
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
195195
sort(words.begin(), words.end(), [&](const string & a, const string & b){
196-
return a.size() < b.size();
196+
return a.size() < b.size();
197197
});
198198
vector<string> ans;
199199
for (auto& w : words)

solution/0400-0499/0472.Concatenated Words/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public:
167167

168168
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
169169
sort(words.begin(), words.end(), [&](const string & a, const string & b){
170-
return a.size() < b.size();
170+
return a.size() < b.size();
171171
});
172172
vector<string> ans;
173173
for (auto& w : words)

solution/0500-0599/0527.Word Abbreviation/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Trie:
102102
def __init__(self):
103103
self.children = [None] * 26
104104
self.v = Counter()
105-
105+
106106
def insert(self, w):
107107
node = self
108108
for c in w:
@@ -111,7 +111,7 @@ class Trie:
111111
node.children[idx] = Trie()
112112
node = node.children[idx]
113113
node.v[w[-1]] += 1
114-
114+
115115
def search(self, w):
116116
node = self
117117
res = []

0 commit comments

Comments
 (0)