Skip to content

Commit 367474c

Browse files
committed
0200-number-of-islands-2.md Push or Enqueue without inside an iteration of 4 items.
1 parent f146f00 commit 367474c

2 files changed

Lines changed: 76 additions & 78 deletions

File tree

solutions/0200-number-of-islands-2.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Solution:
100100
for j, value in enumerate(row):
101101
if value == '1':
102102
island_count += 1
103+
103104
self.depth_first_search((i, j))
104105

105106
return island_count
@@ -121,8 +122,10 @@ class Solution:
121122

122123
self.grid[i][j] = 'V'
123124

124-
for adjacent_point in [(i, j - 1), (i + 1, j), (i, j + 1), (i - 1, j)]:
125-
self.point_stack.append(adjacent_point)
125+
self.point_stack.append((i, j - 1))
126+
self.point_stack.append((i + 1, j))
127+
self.point_stack.append((i, j + 1))
128+
self.point_stack.append((i - 1, j))
126129
```
127130

128131
# Java
@@ -170,11 +173,10 @@ class Solution {
170173

171174
grid[i][j] = 'V';
172175

173-
int[][] adjacentPoints = {{i, j - 1}, {i + 1, j}, {i, j + 1}, {i - 1, j}};
174-
175-
for (var adjacentPoint : adjacentPoints) {
176-
pointStack.push(adjacentPoint);
177-
}
176+
pointStack.push(new int[]{i, j - 1});
177+
pointStack.push(new int[]{i + 1, j});
178+
pointStack.push(new int[]{i, j + 1});
179+
pointStack.push(new int[]{i - 1, j});
178180
}
179181
}
180182
}
@@ -185,17 +187,17 @@ class Solution {
185187
class Solution {
186188
private:
187189
vector<vector<char>> grid_;
188-
stack<vector<int>> point_stack;
190+
stack<pair<int, int>> point_stack;
189191

190-
void depth_first_search(vector<int> point) {
191-
point_stack.push(point);
192+
void depth_first_search(int i1, int j1) {
193+
point_stack.push({i1, j1});
192194

193195
while (!point_stack.empty()) {
194-
point = point_stack.top();
196+
pair<int, int> point = point_stack.top();
195197
point_stack.pop();
196198

197-
int i = point[0];
198-
int j = point[1];
199+
int i = point.first;
200+
int j = point.second;
199201

200202
if (i < 0 || i >= grid_.size()) {
201203
continue;
@@ -211,11 +213,10 @@ private:
211213

212214
grid_[i][j] = 'V';
213215

214-
vector<vector<int>> adjacent_points = {{i, j - 1}, {i + 1, j}, {i, j + 1}, {i - 1, j}};
215-
216-
for (auto adjacent_point : adjacent_points) {
217-
point_stack.push(adjacent_point);
218-
}
216+
point_stack.push({i, j - 1});
217+
point_stack.push({i + 1, j});
218+
point_stack.push({i, j + 1});
219+
point_stack.push({i - 1, j});
219220
}
220221
}
221222

@@ -229,7 +230,7 @@ public:
229230
if (grid_[i][j] == '1') {
230231
island_count++;
231232

232-
depth_first_search({i, j});
233+
depth_first_search(i, j);
233234
}
234235
}
235236
}
@@ -240,7 +241,7 @@ public:
240241
```
241242

242243
# JavaScript
243-
```javascript
244+
```JavaScript
244245
let grid
245246
let pointStack
246247

@@ -282,9 +283,10 @@ function depthFirstSearch(point) {
282283

283284
grid[i][j] = 'V';
284285

285-
[[i, j - 1], [i + 1, j], [i, j + 1], [i - 1, j]].forEach(
286-
(adjacentPoint) => pointStack.push(adjacentPoint)
287-
)
286+
pointStack.push([i, j - 1])
287+
pointStack.push([i + 1, j])
288+
pointStack.push([i, j + 1])
289+
pointStack.push([i - 1, j])
288290
}
289291
}
290292
```
@@ -342,11 +344,10 @@ public class Solution
342344

343345
grid[i][j] = 'V';
344346

345-
int[][] adjacentPoints = [[i, j - 1], [i + 1, j], [i, j + 1], [i - 1, j]];
346-
347-
foreach (var adjacentPoint in adjacentPoints) {
348-
pointStack.Push(adjacentPoint);
349-
}
347+
pointStack.Push([i, j - 1]);
348+
pointStack.Push([i + 1, j]);
349+
pointStack.Push([i, j + 1]);
350+
pointStack.Push([i - 1, j]);
350351
}
351352
}
352353
}
@@ -398,11 +399,10 @@ func depthFirstSearch(point []int) {
398399

399400
grid[i][j] = 'V'
400401

401-
adjacentPoints := [][]int{{i, j - 1}, {i + 1, j}, {i, j + 1}, {i - 1, j}}
402-
403-
for _, adjacentPoint := range adjacentPoints {
404-
pointStack.Push(adjacentPoint)
405-
}
402+
pointStack.Push([]int{i, j - 1})
403+
pointStack.Push([]int{i + 1, j})
404+
pointStack.Push([]int{i, j + 1})
405+
pointStack.Push([]int{i - 1, j})
406406
}
407407
}
408408
```
@@ -444,9 +444,10 @@ def depth_first_search(point)
444444

445445
@grid[i][j] = 'V'
446446

447-
[[i, j - 1], [i + 1, j], [i, j + 1], [i - 1, j]].each do |point|
448-
@point_stack << point
449-
end
447+
@point_stack << [i, j - 1]
448+
@point_stack << [i + 1, j]
449+
@point_stack << [i, j + 1]
450+
@point_stack << [i - 1, j]
450451
end
451452
end
452453
```

solutions/0200-number-of-islands-3.md

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,10 @@ class Solution:
109109

110110
self.grid[i][j] = 'V'
111111

112-
for adjacent_point in [
113-
(i - 1, j),
114-
(i, j + 1),
115-
(i + 1, j),
116-
(i, j - 1),
117-
]:
118-
self.point_queue.append(adjacent_point)
112+
self.point_queue.append((i - 1, j))
113+
self.point_queue.append((i, j + 1))
114+
self.point_queue.append((i + 1, j))
115+
self.point_queue.append((i, j - 1))
119116
```
120117

121118
## Java
@@ -163,11 +160,10 @@ class Solution {
163160

164161
grid[i][j] = 'V';
165162

166-
int[][] adjacentPoints = {{i - 1, j}, {i, j + 1}, {i + 1, j}, {i, j - 1}};
167-
168-
for (var adjacentPoint : adjacentPoints) {
169-
pointQueue.add(adjacentPoint);
170-
}
163+
pointQueue.add(new int[]{i - 1, j});
164+
pointQueue.add(new int[]{i, j + 1});
165+
pointQueue.add(new int[]{i + 1, j});
166+
pointQueue.add(new int[]{i, j - 1});
171167
}
172168
}
173169
}
@@ -178,17 +174,17 @@ class Solution {
178174
class Solution {
179175
private:
180176
vector<vector<char>> grid_;
181-
queue<vector<int>> point_queue;
177+
queue<pair<int, int>> point_queue;
182178

183-
void breadth_first_search(vector<int> point) {
184-
point_queue.push(point);
179+
void breadth_first_search(int i1, int j1) {
180+
point_queue.push({i1, j1});
185181

186182
while (!point_queue.empty()) {
187-
point = point_queue.front();
183+
pair<int, int> point = point_queue.front();
188184
point_queue.pop();
189185

190-
int i = point[0];
191-
int j = point[1];
186+
int i = point.first;
187+
int j = point.second;
192188

193189
if (i < 0 || i >= grid_.size()) {
194190
continue;
@@ -204,11 +200,10 @@ private:
204200

205201
grid_[i][j] = 'V';
206202

207-
vector<vector<int>> adjacent_points = {{i - 1, j}, {i, j + 1}, {i + 1, j}, {i, j - 1}};
208-
209-
for (auto adjacent_point : adjacent_points) {
210-
point_queue.push(adjacent_point);
211-
}
203+
point_queue.push({i - 1, j});
204+
point_queue.push({i, j + 1});
205+
point_queue.push({i + 1, j});
206+
point_queue.push({i, j - 1});
212207
}
213208
}
214209

@@ -222,7 +217,7 @@ public:
222217
if (grid_[i][j] == '1') {
223218
island_count++;
224219

225-
breadth_first_search({i, j});
220+
breadth_first_search(i, j);
226221
}
227222
}
228223
}
@@ -233,13 +228,13 @@ public:
233228
```
234229

235230
## JavaScript
236-
```javascript
231+
```JavaScript
237232
let grid
238233
let pointQueue
239234

240235
var numIslands = function (grid_) {
241236
grid = grid_
242-
pointQueue = new Queue() // https://github.com/datastructures-js/queue
237+
pointQueue = new Queue() // github.com/datastructures-js/queue
243238
let islandCount = 0
244239

245240
grid.forEach((row, i) => {
@@ -275,9 +270,10 @@ function breadthFirstSearch(point) {
275270

276271
grid[i][j] = 'V';
277272

278-
[[i - 1, j], [i, j + 1], [i + 1, j], [i, j - 1]].forEach(
279-
(adjacentPoint) => pointQueue.enqueue(adjacentPoint)
280-
)
273+
pointQueue.enqueue([i - 1, j])
274+
pointQueue.enqueue([i, j + 1])
275+
pointQueue.enqueue([i + 1, j])
276+
pointQueue.enqueue([i, j - 1])
281277
}
282278
}
283279
```
@@ -324,22 +320,23 @@ public class Solution
324320
{
325321
continue;
326322
}
323+
327324
if (j < 0 || j >= grid[0].Length)
328325
{
329326
continue;
330327
}
328+
331329
if (grid[i][j] != '1')
332330
{
333331
continue;
334332
}
335333

336334
grid[i][j] = 'V';
337335

338-
int[][] adjacentPoints = [[i - 1, j], [i, j + 1], [i + 1, j], [i, j - 1]];
339-
340-
foreach (var adjacentPoint in adjacentPoints) {
341-
pointQueue.Enqueue(adjacentPoint);
342-
}
336+
pointQueue.Enqueue([i - 1, j]);
337+
pointQueue.Enqueue([i, j + 1]);
338+
pointQueue.Enqueue([i + 1, j]);
339+
pointQueue.Enqueue([i, j - 1]);
343340
}
344341
}
345342
}
@@ -391,11 +388,10 @@ func breadthFirstSearch(point []int) {
391388

392389
grid[i][j] = 'V'
393390

394-
adjacentPoints := [][]int{{i - 1, j}, {i, j + 1}, {i + 1, j}, {i, j - 1}}
395-
396-
for _, adjacentPoint := range adjacentPoints {
397-
pointQueue.Enqueue(adjacentPoint)
398-
}
391+
pointQueue.Enqueue([]int{i - 1, j})
392+
pointQueue.Enqueue([]int{i, j + 1})
393+
pointQueue.Enqueue([]int{i + 1, j})
394+
pointQueue.Enqueue([]int{i, j - 1})
399395
}
400396
}
401397
```
@@ -437,9 +433,10 @@ def breadth_first_search(point)
437433

438434
@grid[i][j] = 'V'
439435

440-
[[i - 1, j], [i, j + 1], [i + 1, j], [i, j - 1]].each do |point|
441-
@point_queue << point
442-
end
436+
@point_queue << [i - 1, j]
437+
@point_queue << [i, j + 1]
438+
@point_queue << [i + 1, j]
439+
@point_queue << [i, j - 1]
443440
end
444441
end
445442
```

0 commit comments

Comments
 (0)