[LC] 694. Number of Distinct Islands

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.

class Solution {
    int gRow;
    int gCol;
    public int numDistinctIslands(int[][] grid) {
        Set<String> set = new HashSet<>();
        gRow = grid.length;
        gCol = grid[0].length;
        for (int i = 0; i < gRow; i++) {
            for (int j = 0; j < gCol; j++) {
                if (grid[i][j] == 1) {
                    StringBuilder sb = new StringBuilder();
                    dfs(i, j, sb, "o", grid);
                    set.add(sb.toString());
                }
            }
        }
        return set.size();
    }

    private void dfs(int row, int col, StringBuilder sb, String dir, int[][] grid) {
        if (row < 0 || row >= gRow || col < 0 || col >= gCol || grid[row][col] == 0) {
            return;
        }
        grid[row][col] = 0;
        sb.append(dir);
        dfs(row + 1, col, sb, "r", grid);
        dfs(row - 1, col, sb, "l", grid);
        dfs(row, col + 1, sb, "u", grid);
        dfs(row, col - 1, sb, "d", grid);
        // need to declarative backtrack
        sb.append("b");
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12701442.html

时间: 2024-11-05 10:23:45

[LC] 694. Number of Distinct Islands的相关文章

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

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 island

694. Number of Distinct Islands - Medium

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 island

[LeetCode] 694. Number of Distinct Islands

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 island

[LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

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 island

SPOJ 694、705 Distinct Substrings 、 New Distinct Substrings (后缀数组)

题目大意: 求串中不同的子串的个数. 思路分析: 子串一定是某一个后缀的前缀. 所以我们把每一个后缀拿出来,分析它有多少个前缀,然后除去它与sa数组中前面那个后缀相同的前缀. 最后也就是 ans = segma (n-sa[i] + height[i]).... #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define maxn 1000005

[LC] 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 5257. 统计封闭岛屿的数目 Number of Closed Islands

地址 https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/ 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座「岛屿」. 如果一座岛屿 完全 由水域包围,即陆地边缘上下左右所有相邻区域都是水域,那么我们将其称为 「封闭岛屿」. 请返回封闭岛

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder