Skip to content

Commit

Permalink
Update LeetCode Post " LeetCode : 200. Number of Islands "
Browse files Browse the repository at this point in the history
  • Loading branch information
goodGid committed Mar 1, 2024
1 parent 763ae84 commit 0bbdeb5
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions _posts/2021-12-05-LeetCode-Number-of-Islands.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,95 @@ class Solution {
* 단순 구현 + BFS 풀이

---

### [2] Code (24. 03. 01)

*Retry -> 풀긴했는데 코드가 더럽다.*

``` java
// Runtime: 9 ms
// Memory Usage: 54.1 MB
// Ref : https://leetcode.com/submissions/detail/1190639057
class Solution {
public int numIslands(char[][] _grid) {
int[] dx = { 1, -1, 0, 0 };
int[] dy = { 0, 0, 1, -1 };

int n = _grid.length;
int m = _grid[0].length;

Queue<int[]> q = new LinkedList<>();
int[][] grid = new int[n][m];
int[][] visit = new int[n][m];

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = _grid[i][j] - '0';
if (grid[i][j] == 1) {
q.add(new int[] { i, j });
}
}
}

int cnt = 2;

while (!q.isEmpty()) {
int[] _node = q.poll();
int _x = _node[0];
int _y = _node[1];

if (grid[_x][_y] != 1) {
continue;
}
grid[_x][_y] = cnt;
Queue<int[]> q2 = new LinkedList<>();
q2.add(new int[] { _x, _y });

while (!q2.isEmpty()) {
int[] node = q2.poll();
int x = node[0];
int y = node[1];

for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

if (!isLand(grid, nx, ny)) {
continue;
}
grid[nx][ny] = cnt;
q2.add(new int[] { nx, ny });
}
}
cnt++;
}
return cnt - 2;
}

private boolean isLand(int[][] map, int x, int y) {
int n = map.length;
int m = map[0].length;
if (x < 0 || x >= n || y < 0 || y >= m) {
return false;
}

if (map[x][y] == 0 || map[x][y] != 1) {
return false;
}

return true;
}
}
```

* 풀긴했는데 코드가 깔끔하지 못해서 마음에 안든다.

[1]에 풀었던 코드를 보니 훨씬 깔끔하다.

~~코드를 깔끔하게 짜는 설계 능력이 퇴보했다.~~

* 위안을 삼자면 그나마 제출 1번에 바로 맞췄다.

---

Expand Down

0 comments on commit 0bbdeb5

Please sign in to comment.