当前位置 : 主页 > 大数据 > 区块链 >

[LeetCode] 694. Number of Distinct Islands

来源:互联网 收集:自由互联 发布时间:2021-06-22
Given a non-empty 2D array grid of 0‘s and 1‘s, anislandis a group of 1 ‘s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number ofd

Given a non-empty 2D array grid of 0‘s and 1‘s, an island is a group of 1‘s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.

Example 1:

11000
11000
00011
00011

Given the above grid map, return 1.

 

Example 2:

11011
10000
00001
11011

Given the above grid map, return 3.

Notice that:

11
1

and

 1
11

are considered different island shapes, because we do not consider reflection / rotation.

 

Note: The length of each dimension in the given grid does not exceed 50.

 

这个题目可以理解为是[LeetCode] 200. Number of Islands_ Medium tag: BFS的变形, 不仅仅是要判断有多少个island, 并且是要unique的, 不能翻转, 那么如果做呢, 因为我们scan的时候是一行一行得scan, 用DFS去scan的时候也有它的先后顺序, 所以可以通过每次用DFS的时候将路径记下来, 并且add进入一个ans的set里面, 最后返回len(ans), 就是distinct islands的数目了.

 

updated at 07/10, 参考题目的Solution, 思路跟之前类似, 只不过, 每个island我们将坐标都变为local的坐标, 相当于以最开始的坐标作为原点坐标, 获得相对坐标的位置, 然后将该路径记下来, 并且将其变为tuple然后add进入一个ans的set里面, 最后返回len(ans).

1. Constraints

1) grid can be empty,

2) grid size <= 50 * 50

3) element will only be 0 or 1

 

2. Ideas

DFS:      T: O(m*n)      S: O(m*n)

1) edge case, not grid or len(grid[0]) == 0, return 0

2) grid[i][j] if not visited, dfs(得到它的路径), 并add进入ans 的set

3) 对于dfs, 记住路径, 并且append进入dirs

4) 返回len(ans)

 

3. Code

 1 class Solution:
 2     def numDistinctIslands(self, grid):
 3         if not gird or len(grid[0]) == 0: return 0
 4         lr, lc, ans, visited = len(grid), len(grid[0]), set(), set()
 5 
 6         def dfs(grid, i, j, visited, dirs, dirt):
 7             if 0<= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1 and (i, j) not in visited:
 8                 visited.add((i,j))
 9                 dirs.append(dirt)
10                 dfs(grid, i-1, j, visited, dirs, u)
11                 dfs(grid, i+1, j, visited, dirs, d)
12                 dfs(grid, i, j-1, visited, dirs, l)
13                 dfs(grid, i, j+1, visited, dirs, r)
14                 dirs.append(b) # if no this line, [[110],[011],[000],[111],[010]] can not pass
15         for i in range(lr):
16             for j in range(lc):
17                 if grid[i][j] == 1 and (i,j) not in visited:
18                     dirs = []
19                     dfs(grid, i, j,visited, dirs, o )
20                     ans.add("".join(dirs))
21         return len(ans)

 

3.1  Code(利用loca 相对坐标位置)

 1 class Solution:
 2     def numDistinctIsland(self, grid):
 3         if not grid or len(grid[0]) == 0: return 0
 4         def dfs(grid, r, c, r0, c0, visited, shape):
 5             if 0 <= r < len(grid) and 0<= c < len(grid[0]) and grid[r][c] == 1 and (r, c) not in visited:
 6             visited.add((r,c))
 7             shape.append((r-r0, c-c0))
 8             dfs(grid, r+1, c, r0, c0, visited, shape)
 9             dfs(grid, r-1, c, r0, c0, visited, shape)
10             dfs(grid, r, c+1, r0, c0, visited, shape)
11             dfs(grid, r, c-1, r0, c0, visited, shape)
12         lr, lc, ans, visited = len(grid), len(grid[0]), set(), set()
13         for i in range(lr):
14             for j in range(lc):
15                 if grid[i][j] == 1 and (i, j) not in visited:
16                     shape = []
17                     dfs(grid, i, j, i, j , visited, shape)
18                     ans.add(tuple(shape))
19         return len(ans)

 

 

4. Test cases

1) edge cases

2) [[110],[011],[000],[111],[010]]

3) 

11011
10000
00001
11011
网友评论