java 694. Distinct Islands.java的数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 694. Distinct Islands.java的数量相关的知识,希望对你有一定的参考价值。

class Solution {
    public int numDistinctIslands(int[][] grid) {
        if (grid == null || grid.length < 1 || grid[0].length < 1) return 0;
        int m = grid.length, n = grid[0].length;
        Set<String> res = new HashSet<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                Set<String> set = new HashSet<>();
                if (grid[i][j] == 1) {
                    dfs(grid, i, j, i, j, set);
                    res.add(set.toString());
                }
            }
        }
        return res.size();
    }
    
    public void dfs(int[][] grid, int i, int j, int baseX, int baseY, Set<String> set) {
        int m = grid.length, n = grid[0].length;
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) return;
        set.add((i - baseX) + "_" + (j - baseY));
        grid[i][j] = 0;
        dfs(grid, i + 1, j, baseX, baseY, set);
        dfs(grid, i - 1, j, baseX, baseY, set);
        dfs(grid, i, j - 1, baseX, baseY, set);
        dfs(grid, i, j + 1, baseX, baseY, set);
    }
}

以上是关于java 694. Distinct Islands.java的数量的主要内容,如果未能解决你的问题,请参考以下文章

[LC] 694. Number of Distinct Islands

[leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

java 694. Distinct Islands.java的数量

java 694. Distinct Islands.java的数量

Spoj 694 Distinct Substrings

SPOJ694/DISUBSTR???Distinct Substrings????????????