美文网首页
694. Number of Distinct Islands

694. Number of Distinct Islands

作者: Nancyberry | 来源:发表于2018-05-17 07:22 被阅读0次

Description

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.

Solution

DFS + HashSet, time O(mn), space O(mn)

explore一个island没什么难的,用DFS就行,本题的难点在如何判断两个island相等。

考虑如何表示一个island,island其实就是一个不规则的多边形,用对角线?用顶点?感觉都不行,而且很麻烦。还是笨一点,直接枚举island中的所有节点位置。

那么可以用List<Integer> shape来表示一个island,shape中存储的是2D转成1D的坐标。考虑如果判断List<Integer>相等,看javadoc怎么说:

public boolean equals(Object o)

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.

这就好办了。对于每个island,我们都从它的左上角开始explore,并将所有坐标都换算成与左上角的相对坐标,并按照一定的顺序加入shape中。这样如果两个island的形状相同,他们的shape必然equal。那么用HashSet就可以做到去重啦。

class Solution {
    public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    public int numDistinctIslands(int[][] grid) {
        Set<List<Integer>> shapes = new HashSet<>();
        
        for (int i = 0; i < grid.length; ++i) {
            for (int j = 0; j < grid[0].length; ++j) {
                List<Integer> shape = new ArrayList<>();
                if (dfs(grid, i, j, i, j, shape)) {
                    shapes.add(shape);
                }
            }
        }
        
        return shapes.size();
    }
    // return true if (i, j) belongs to an unvisited island, otherwise return false
    private boolean dfs(int[][] grid, int i0, int j0, int i, int j, List<Integer> shape) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) {
            return false;
        }
        
        grid[i][j] = -1;
        shape.add(getIndex(grid, i - i0, j - j0));
        
        for (int[] d : DIRECTIONS) {
            dfs(grid, i0, j0, i + d[0], j + d[1], shape);
        }
        
        return true;
    }
    
    private int getIndex(int[][] grid, int i, int j) {
        return i * grid[0].length + j;
    }
}

Serialize island + DFS + HashSet, time O(mn), space O(mn)

比上面做法更妙的方式,是将island序列化成一个String!

class Solution {
    public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    public int numDistinctIslands(int[][] grid) {
        Set<String> shapes = new HashSet<>();
        
        for (int i = 0; i < grid.length; ++i) {
            for (int j = 0; j < grid[0].length; ++j) {
                StringBuilder sb = new StringBuilder();
                
                if (dfs(grid, i, j, i, j, sb)) {
                    shapes.add(sb.toString());
                }
            }
        }
        
        return shapes.size();
    }
    // return true if (i, j) belongs to an unvisited island, otherwise return false
    private boolean dfs(int[][] grid, int i0, int j0, int i, int j, StringBuilder sb) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) {
            return false;
        }
        
        grid[i][j] = -1;
        sb.append(getIndex(grid, i - i0, j - j0)).append(",");
        
        for (int[] d : DIRECTIONS) {
            dfs(grid, i0, j0, i + d[0], j + d[1], sb);
        }
        
        return true;
    }
    
    private int getIndex(int[][] grid, int i, int j) {
        return i * grid[0].length + j;
    }
}

相关文章

网友评论

      本文标题:694. Number of Distinct Islands

      本文链接:https://www.haomeiwen.com/subject/vppvdftx.html