Skip to content

Commit

Permalink
fix java 200, 495, cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 2, 2023
1 parent 5dcb97f commit 4abe53e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 66 deletions.
4 changes: 2 additions & 2 deletions doc/cheatsheet/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
- is_cycle


#### 1-1-1) Number of Distinct Islands
#### 1-1-1) Number of Islands

- LC 694
- LC 200

```java
// java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ void dfs(int[][] grid, int x, int y, boolean[][] visited, int ox, int oy, List<L
dfs(grid, nextx, nexty, visited, ox, oy, allList);
}
}

public static void main(String[] args) {
//System.out.println(true + false);
}
}
116 changes: 52 additions & 64 deletions leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,58 @@ public class NumberOfIslands {

// V0
// IDEA : DFS
// TODO : fix below
// int ans = 0;
// public int numIslands(char[][] grid) {
//
// if (grid.length == 0 && grid[0].length == 0){
// return 0;
// }
//
// int _len = grid.length;
// int _width = grid[0].length;
//
// // get all "1"
// List<List<Integer>> toVisit = new ArrayList();
// for (int i = 0; i < _width; i++){
// for (int j = 0; j < _len; j++){
// String val = String.valueOf(grid[j][i]);
// if (val == "1"){
// toVisit.add(Arrays.asList(j, i));
// }
// }
// }
//
// // dfs
// for (List<Integer> point : toVisit){
// int _x = point.get(1);
// int _y = point.get(0);
// if (_dfs(grid, _x, _y)){
// this.ans += 1;
// }
// }
//
// return this.ans;
// }
//
// private boolean _dfs(char[][] grid, int x, int y){
//
// int _len = grid.length;
// int _width = grid[0].length;
//
// String val = String.valueOf(grid[y][x]);
// if (val == "#" || val == "0"){
// return false;
// }
//
// List<List<Integer>> diresctions = new ArrayList();
// diresctions.add(Arrays.asList(0, 1));
// diresctions.add(Arrays.asList(0, -1));
// diresctions.add(Arrays.asList(1, 0));
// diresctions.add(Arrays.asList(-1, 0));
//
// for (List<Integer> direction : diresctions){
//
// x += direction.get(1);
// y += direction.get(0);
//
// if (x >= 0 && x < _width && y >= 0 && y < _len){
// // https://stackoverflow.com/questions/5859934/char-initial-value-in-java
// grid[y][x] = '#';
// _dfs(grid, x, y);
// }
//
// }
// return true;
// }
int num_island = 0;
boolean[][] _seen;
public int numIslands(char[][] grid) {

if (grid.length == 1 && grid[0].length == 1){
if (grid[0][0] == '1'){
return 1;
}
return 0;
}

int len = grid.length;
int width = grid[0].length;

// NOTE !!! how we init M X N boolean matrix
this._seen = new boolean[len][width];

for (int i = 0; i < len; i++){
for (int j = 0; j < width; j++){
if (_is_island(grid, j, i, this._seen)){
this.num_island += 1;
}
}
}

return this.num_island;
}

private boolean _is_island(char[][] grid, int x, int y, boolean[][] seen){

int len = grid.length;
int width = grid[0].length;

// NOTE !!! boundary condition : x >= width, y >= len
// since index = lenth - 1
if (x < 0 || x >= width || y < 0 || y >= len || this._seen[y][x] == true || grid[y][x] == '0'){
return false;
}

this._seen[y][x] = true;

/** NOTE !!! we do 4 direction traverse on the same time */
_is_island(grid, x+1, y, seen);
_is_island(grid, x-1, y, seen);
_is_island(grid, x, y+1, seen);
_is_island(grid, x, y-1, seen);

// NOTE !!! if code can arrive here, means there is at least "1 direction" meet "1" value
// -> there is an island
// -> so we return true as we found an island
return true;
}

// V1
// IDEA : DFS
Expand Down
48 changes: 48 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace1.java
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,52 @@ private int _getArea(int[][] grid, boolean[][] seen, int x, int y){
_getArea(grid, seen, x, y-1);
}

// https://leetcode.com/problems/number-of-islands/description/

int num_island = 0;
boolean[][] _seen;
public int numIslands(char[][] grid) {

if (grid.length == 1 && grid[0].length == 1){
if (grid[0][0] == '1'){
return 1;
}
return 0;
}

int len = grid.length;
int width = grid[0].length;

this._seen = new boolean[len][width];

for (int i = 0; i < len; i++){
for (int j = 0; j < width; j++){
if (_is_island(grid, j, i, this._seen)){
this.num_island += 1;
}
}
}

return this.num_island;
}

private boolean _is_island(char[][] grid, int x, int y, boolean[][] seen){

int len = grid.length;
int width = grid[0].length;

if (x < 0 || x >= width || y < 0 || y >= len || this._seen[y][x] == true || grid[y][x] == '0'){
return false;
}

this._seen[y][x] = true;

_is_island(grid, x+1, y, seen);
_is_island(grid, x-1, y, seen);
_is_island(grid, x, y+1, seen);
_is_island(grid, x, y-1, seen);

return true;
}

}

0 comments on commit 4abe53e

Please sign in to comment.