File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,37 @@ Output: 3
5353
5454## 代码
5555
56- * 语言支持:JS, python3
56+ * 语言支持:JS, python3,Java
57+
58+ Java Code:
59+
60+ ``` java
61+ public int numIslands(char [][] grid) {
62+ if (grid == null || grid. length == 0 || grid[0 ]. length == 0 ) return 0 ;
63+
64+ int count = 0 ;
65+ for (int row = 0 ; row < grid. length; row++ ) {
66+ for (int col = 0 ; col < grid[0 ]. length; col++ ) {
67+ if (grid[row][col] == ' 1' ) {
68+ dfs(grid, row, col);
69+ count++ ;
70+ }
71+ }
72+ }
73+ return count;
74+ }
75+
76+ private void dfs(char [][] grid,int row,int col) {
77+ if (row< 0 || row== grid. length|| col< 0 || col== grid[0 ]. length|| grid[row][col]!= ' 1' ) {
78+ return ;
79+ }
80+ grid[row][col] = ' 0' ;
81+ dfs(grid, row- 1 , col);
82+ dfs(grid, row+ 1 , col);
83+ dfs(grid, row, col+ 1 );
84+ dfs(grid, row, col- 1 );
85+ }
86+ ```
5787
5888Javascript Code:
5989``` js
You can’t perform that action at this time.
0 commit comments