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 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.

hashtable + dfs

在dfs找完整islands时,用StringBuilder存island增长的path,来表示其形状(从遍历到的第一个为1的点开始,每一个点向上/下/左/右走)

比如说 11 的形状是00 10 01,  1 的的形状是00 10 1-1

          1          11

每找到一个就存进set,防止重复。最后返回set的大小。

可以不用visited数组,直接将访问过的1赋值为0即可

时间:O(M*N),空间:O(M*N)

class Solution {
    int[][] dirs = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

    public int numDistinctIslands(int[][] grid) {
        if(grid == null || grid.length == 0) return 0;
        int m = grid.length, n = grid[0].length;
        Set<String> set = new HashSet<>();
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 1) {
                    StringBuilder sb = new StringBuilder();
                    dfs(grid, i, j, 0, 0, sb);
                    if(!set.contains(sb.toString()))
                        set.add(sb.toString());
                }
            }
        }
        return set.size();
    }

    public void dfs(int[][] grid, int i, int j, int xpos, int ypos, StringBuilder sb) {
        grid[i][j] = 0;
        sb.append(xpos + "" + ypos);
        for(int[] dir : dirs) {
            int x = i + dir[0], y = j + dir[1];
            if(x < 0 || x > grid.length - 1 || y < 0 || y > grid[0].length - 1 || grid[x][y] == 0)
                continue;
            dfs(grid, x, y, xpos + dir[0], ypos + dir[1], sb);
        }
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10040079.html

时间: 2024-10-29 17:26:16

694. Number of Distinct Islands - Medium的相关文章

[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] 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

[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 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

200. Number of Islands - Medium

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

leetcode750 - Number Of Corner Rectangles - medium

Given a grid where each entry is only 0 or 1, find the number of corner rectangles.A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle. Note that only the corners need to have the value 1. Also, all four 1s used must b

136 Single Number(找唯一数Medium)

题目意思:一个int数组,有一个数只出现一次,其他数均出现两次,找到这个唯一数 知识普及:~:非运算,单目运算符1为0,0为1;   &:与运算,都为1则为1,否则为0 |:或运算,全为0则为0,否则为1        ^:异或运算,相同为0,不同为1 思路:将数组中元素进行异或运算,则只剩下0和唯一数,异或得到的是唯一数 1 class Solution { 2 public: 3 int singleNumber(vector<int>& nums) { 4 int res

LEETCODE 5257. 统计封闭岛屿的数目 Number of Closed Islands

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