Skip to content

Commit a477623

Browse files
committed
feat: update solutions to lc problems: No.1032,1034
* No.1032.Stream of Characters * No.1034.Coloring A Border
1 parent d8ec3b6 commit a477623

File tree

9 files changed

+115
-14
lines changed

9 files changed

+115
-14
lines changed

solution/1000-1099/1032.Stream of Characters/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class Trie {
147147

148148
public boolean query(StringBuilder s) {
149149
Trie node = this;
150-
for (int i = s.length() - 1, j = 0; i >= 0 && j < 201; --i, ++j) {
150+
for (int i = s.length() - 1; i >= 0; --i) {
151151
int idx = s.charAt(i) - 'a';
152152
if (node.children[idx] == null) {
153153
return false;
@@ -211,7 +211,7 @@ public:
211211

212212
bool search(string& w) {
213213
Trie* node = this;
214-
for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) {
214+
for (int i = w.size() - 1; ~i; --i) {
215215
int idx = w[i] - 'a';
216216
if (!node->children[idx]) {
217217
return false;
@@ -231,7 +231,7 @@ public:
231231
string s;
232232

233233
StreamChecker(vector<string>& words) {
234-
for (auto& w : words) {
234+
for (auto&& w : words) {
235235
trie->insert(w);
236236
}
237237
}
@@ -275,7 +275,7 @@ func (this *Trie) Insert(word string) {
275275
276276
func (this *Trie) Search(word []byte) bool {
277277
node := this
278-
for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 {
278+
for i := len(word) - 1; i >= 0; i-- {
279279
idx := word[i] - 'a'
280280
if node.children[idx] == nil {
281281
return false

solution/1000-1099/1032.Stream of Characters/README_EN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Trie {
124124

125125
public boolean query(StringBuilder s) {
126126
Trie node = this;
127-
for (int i = s.length() - 1, j = 0; i >= 0 && j < 201; --i, ++j) {
127+
for (int i = s.length() - 1; i >= 0; --i) {
128128
int idx = s.charAt(i) - 'a';
129129
if (node.children[idx] == null) {
130130
return false;
@@ -188,7 +188,7 @@ public:
188188

189189
bool search(string& w) {
190190
Trie* node = this;
191-
for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) {
191+
for (int i = w.size() - 1; ~i; --i) {
192192
int idx = w[i] - 'a';
193193
if (!node->children[idx]) {
194194
return false;
@@ -208,7 +208,7 @@ public:
208208
string s;
209209

210210
StreamChecker(vector<string>& words) {
211-
for (auto& w : words) {
211+
for (auto&& w : words) {
212212
trie->insert(w);
213213
}
214214
}
@@ -252,7 +252,7 @@ func (this *Trie) Insert(word string) {
252252
253253
func (this *Trie) Search(word []byte) bool {
254254
node := this
255-
for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 {
255+
for i := len(word) - 1; i >= 0; i-- {
256256
idx := word[i] - 'a'
257257
if node.children[idx] == nil {
258258
return false

solution/1000-1099/1032.Stream of Characters/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Trie {
2222

2323
bool search(string& w) {
2424
Trie* node = this;
25-
for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) {
25+
for (int i = w.size() - 1; ~i; --i) {
2626
int idx = w[i] - 'a';
2727
if (!node->children[idx]) {
2828
return false;
@@ -42,7 +42,7 @@ class StreamChecker {
4242
string s;
4343

4444
StreamChecker(vector<string>& words) {
45-
for (auto& w : words) {
45+
for (auto&& w : words) {
4646
trie->insert(w);
4747
}
4848
}

solution/1000-1099/1032.Stream of Characters/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (this *Trie) Insert(word string) {
2121

2222
func (this *Trie) Search(word []byte) bool {
2323
node := this
24-
for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 {
24+
for i := len(word) - 1; i >= 0; i-- {
2525
idx := word[i] - 'a'
2626
if node.children[idx] == nil {
2727
return false

solution/1000-1099/1032.Stream of Characters/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void insert(String w) {
1616

1717
public boolean query(StringBuilder s) {
1818
Trie node = this;
19-
for (int i = s.length() - 1, j = 0; i >= 0 && j < 201; --i, ++j) {
19+
for (int i = s.length() - 1; i >= 0; --i) {
2020
int idx = s.charAt(i) - 'a';
2121
if (node.children[idx] == null) {
2222
return false;

solution/1000-1099/1034.Coloring A Border/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,42 @@ func colorBorder(grid [][]int, row int, col int, color int) [][]int {
210210
}
211211
```
212212

213+
### **TypeScript**
214+
215+
```ts
216+
function colorBorder(
217+
grid: number[][],
218+
row: number,
219+
col: number,
220+
color: number,
221+
): number[][] {
222+
const m = grid.length;
223+
const n = grid[0].length;
224+
const vis = new Array(m).fill(0).map(() => new Array(n).fill(false));
225+
const dirs = [-1, 0, 1, 0, -1];
226+
const dfs = (i: number, j: number, c: number) => {
227+
vis[i][j] = true;
228+
for (let k = 0; k < 4; ++k) {
229+
const x = i + dirs[k];
230+
const y = j + dirs[k + 1];
231+
if (x >= 0 && x < m && y >= 0 && y < n) {
232+
if (!vis[x][y]) {
233+
if (grid[x][y] == c) {
234+
dfs(x, y, c);
235+
} else {
236+
grid[i][j] = color;
237+
}
238+
}
239+
} else {
240+
grid[i][j] = color;
241+
}
242+
}
243+
};
244+
dfs(row, col, grid[row][col]);
245+
return grid;
246+
}
247+
```
248+
213249
### **...**
214250

215251
```

solution/1000-1099/1034.Coloring A Border/README_EN.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,42 @@ func colorBorder(grid [][]int, row int, col int, color int) [][]int {
176176
}
177177
```
178178

179+
### **TypeScript**
180+
181+
```ts
182+
function colorBorder(
183+
grid: number[][],
184+
row: number,
185+
col: number,
186+
color: number,
187+
): number[][] {
188+
const m = grid.length;
189+
const n = grid[0].length;
190+
const vis = new Array(m).fill(0).map(() => new Array(n).fill(false));
191+
const dirs = [-1, 0, 1, 0, -1];
192+
const dfs = (i: number, j: number, c: number) => {
193+
vis[i][j] = true;
194+
for (let k = 0; k < 4; ++k) {
195+
const x = i + dirs[k];
196+
const y = j + dirs[k + 1];
197+
if (x >= 0 && x < m && y >= 0 && y < n) {
198+
if (!vis[x][y]) {
199+
if (grid[x][y] == c) {
200+
dfs(x, y, c);
201+
} else {
202+
grid[i][j] = color;
203+
}
204+
}
205+
} else {
206+
grid[i][j] = color;
207+
}
208+
}
209+
};
210+
dfs(row, col, grid[row][col]);
211+
return grid;
212+
}
213+
```
214+
179215
### **...**
180216

181217
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function colorBorder(
2+
grid: number[][],
3+
row: number,
4+
col: number,
5+
color: number,
6+
): number[][] {
7+
const m = grid.length;
8+
const n = grid[0].length;
9+
const vis = new Array(m).fill(0).map(() => new Array(n).fill(false));
10+
const dirs = [-1, 0, 1, 0, -1];
11+
const dfs = (i: number, j: number, c: number) => {
12+
vis[i][j] = true;
13+
for (let k = 0; k < 4; ++k) {
14+
const x = i + dirs[k];
15+
const y = j + dirs[k + 1];
16+
if (x >= 0 && x < m && y >= 0 && y < n) {
17+
if (!vis[x][y]) {
18+
if (grid[x][y] == c) {
19+
dfs(x, y, c);
20+
} else {
21+
grid[i][j] = color;
22+
}
23+
}
24+
} else {
25+
grid[i][j] = color;
26+
}
27+
}
28+
};
29+
dfs(row, col, grid[row][col]);
30+
return grid;
31+
}

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md

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

1111
<ul>
1212
<li>Take any bag of balls and divide it into two new bags with a <strong>positive </strong>number of balls.
13-
1413
<ul>
1514
<li>For example, a bag of <code>5</code> balls can become two new bags of <code>1</code> and <code>4</code> balls, or two new bags of <code>2</code> and <code>3</code> balls.</li>
1615
</ul>
1716
</li>
18-
1917
</ul>
2018

2119
<p>Your penalty is the <strong>maximum</strong> number of balls in a bag. You want to <strong>minimize</strong> your penalty after the operations.</p>

0 commit comments

Comments
 (0)