Number of Islands

1. Title

2. Http address

https://leetcode.com/problems/palindrome-linked-list/

3. The question

Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000
Answer: 1

Example 2:

11000
11000
00100
00011
Answer: 3

4. My code(AC)

 1     // Accepted
 2        public int numIslands(char[][] grid) {
 3              if ( grid == null || grid.length == 0 )
 4                  return 0;
 5             boolean visited[][] = new boolean[grid.length][grid[0].length];
 6             int count = 0;
 7             for(int i = 0 ; i < grid.length; i++ )
 8             {
 9                 for(int j = 0 ; j <grid[0].length; j++ )
10                 {
11                     if( grid[i][j] == ‘1‘ && visited[i][j] == false )
12                     {
13                         DFS(grid,i,j,visited);
14                         count++;
15                     }
16                 }
17             }
18
19             return count;
20
21         }
22         public  void DFS(char [][]grid, int i ,int j, boolean [][]visited){
23
24                 if( visited[i][j] != false)
25                         return;
26                 int x = 0, y =0;
27                 visited[i][j] = true;
28                 //up
29                 x = i-1;
30                 y = j;
31                 if( x >= 0  && grid[x][y] == ‘1‘ && visited[x][y] == false ){
32                     DFS(grid, x, y ,visited);
33                 }
34
35                 //down
36                 x = i + 1;
37                 y = j;
38                 if( x < grid.length  && grid[x][y] == ‘1‘ && visited[x][y] == false ){
39                     DFS(grid, x, y ,visited);
40                 }
41
42                 //left
43                 x = i;
44                 y = j-1;
45                 if( y >= 0 && grid[x][y] == ‘1‘ && visited[x][y] == false ){
46                     DFS(grid, x, y ,visited);
47                 }
48
49
50                 //right
51                 x = i;
52                 y = j+1;
53                 if( y < grid[0].length  && grid[x][y] == ‘1‘ && visited[x][y] == false ){
54                     DFS(grid, x, y ,visited);
55                 }
56
57         }
时间: 2024-10-11 01:17:46

Number of Islands的相关文章

200. Number of Islands

https://leetcode.com/problems/number-of-islands/#/description Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Yo

Number of Islands II

Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y

Leetcode 200. Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[LeetCode] 200. Number of Islands 解题思路

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

Number of Islands -- leetcode

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

【LeetCode】200. Number of Islands (2 solutions)

Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are

[LeetCode] Number of Islands

Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are

LeetCode OJ:Number of Islands(孤岛计数)

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[LeetCode] Number of Islands II

Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands