@@ -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 {
185187class Solution {
186188private:
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
244245let grid
245246let 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
451452end
452453```
0 commit comments