关闭

LWC 53:694. Number of Distinct Islands

713人阅读 评论(0) 收藏 举报
分类:

LWC 53:694. Number of Distinct Islands

传送门:694. Number of Distinct Islands

Problem:

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:
1 1
1
and
1
1 1
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.

思路:
首先dfs求出每个区域,并记录每个区域的起点,接着根据这些起点再dfs一次,判断四个方向上的长度是否相同。比如:

0 1 0 0 0 1 0
1 1 1 0 1 1 1
0 1 0 0 0 1 0

可以记录每个位置上四个方向的延伸长度,如果每个位置的上的四个值均相同,则说明两个区域重复。

实际上超时,可以采用HASH的思想,在第一次DFS时,就把四个方向的信息取回来,最终存一个起点的HASH值即可。

代码如下:

    int[][] dir = {{1, 0},{0, 1},{-1, 0},{0, -1}};
    int[][] dp = new int[50 * 50 + 16][5]; // four direction

    boolean[][] vis = new boolean[52][52];
    int n, m;

    Set<Integer> set = new HashSet<>();
    public int numDistinctIslands(int[][] grid) {
        n = grid.length;
        if (n == 0) return 0;
        m = grid[0].length;
        if (m == 0) return 0;
        vis = new boolean[52][52];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 1) {
                    if (!vis[i][j]) {
                        si = i;
                        sj = j;
                        dfs(grid, i, j);
                        set.add(dp[i * m + j][4]);
                    }
                }
            }
        }

        return set.size();
    }

    boolean check(int i, int j) {
        return i >= 0 && i < n && j >= 0 && j < m;
    }

    int si, sj;
    public int[] dfs(int[][] grid, int x, int y) {
        vis[x][y] = true;

        for (int k = 0; k < 4; ++k) {
            int[] d = dir[k];
            int nx = d[0] + x;
            int ny = d[1] + y;
            if (check(nx, ny) && !vis[nx][ny] && grid[nx][ny] == 1) {
                dp[x * m + y][k] = 1 + dfs(grid, nx, ny)[k];
                dp[x * m + y][4] += (dp[nx * m + ny][4] + 1) * (k + 1);
            }
        }

        for (int k = 0; k < 4; ++k) {
            dp[x * m + y][4] += (((x - si + 1) * m + (y - sj + 1)) * 177) *  (dp[x * m + y][k] + 1) * (k + 1);
        }

        return dp[x * m + y];
    }

上述代码采用数值的做法,当然,你也可以直接遍历一条链,以字符串的形式存储遍历的顺序,理解起来更直观,代码如下:

    int[][] dir = {{1, 0},{0, 1},{-1, 0},{0, -1}};
    char[]  c   = {'D','R','U','L'};
    int n, m;

    boolean[][] vis;

    public int numDistinctIslands(int[][] grid) {
        n = grid.length;
        if (n == 0) return 0;
        m = grid[0].length;
        if (m == 0) return 0;
        vis = new boolean[n][m];
        Set<String> set = new HashSet<>();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 1 && !vis[i][j]) {
                    si = i;
                    sj = j;
                    set.add(dfs(grid, i, j));
                }
            }
        }
        return set.size();
    }

    int si, sj;
    public String dfs(int[][] grid, int i, int j) {
        String ans = "" + ((i - si) * m + (j - sj));
        vis[i][j] = true;
        for (int k = 0; k < 4; ++k) {
            int[] d = dir[k];
            int ni = d[0] + i;
            int nj = d[1] + j;
            if (check(ni, nj) && !vis[ni][nj] && grid[ni][nj] == 1) {
                ans += c[k] + dfs(grid, ni, nj);
            }
        }
        return ans;
    }

    boolean check(int i, int j) {
        return i >= 0 && i < n && j >= 0 && j < m;
    }

注意:把相对位置的信息带进来,否则个别案例过不了。

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】

【200-Number of Islands(岛的数目)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-25 06:46
  • 3815

win7旗舰版安装 oracle 10g 不能进入图形界面的问题

前阵子重装了系统,把dell机器自带的win7 64位(家庭版已升级旗舰版,装ORACLE正常)换回了32位系统,前两天因为一些软件开发的问题,需要把以前做的一个系统重新架起来,数据库用的是oracl...
  • trampwind
  • trampwind
  • 2013-09-06 12:08
  • 11708

Leetcode全字符问题

目录 1、编号3 Longest Substring Without Repeating Characters 2、编号6 ZigZag Conversion 3、编号20 Valid Parent...
  • wangxiaojun911
  • wangxiaojun911
  • 2014-02-07 08:09
  • 2833

LeetCode 305. Number of Islands II(小岛)

原题网址:https://leetcode.com/problems/number-of-islands-ii/ A 2d grid map of m rows and n columns is...
  • jmspan
  • jmspan
  • 2016-04-19 13:13
  • 1220

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 surro...
  • kakitgogogo
  • kakitgogogo
  • 2016-04-20 00:13
  • 234

Leetcode-200. Number of Islands

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2016-12-30 21:11
  • 142

LeetCodeOJ_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 surro...
  • u012482487
  • u012482487
  • 2015-06-08 23:46
  • 223

leetcode_Number of Islands_medium--dfs

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is sur...
  • u013861066
  • u013861066
  • 2016-02-22 11:13
  • 179

200. Number of Islands+130. Surrounded Regions(并查集/DFS)

首先看200题:Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island ...
  • FreeeLinux
  • FreeeLinux
  • 2017-02-10 20:42
  • 229

(LeetCode 200)Number of Islands(并查集、DFS)

Q: Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is s...
  • u014616233
  • u014616233
  • 2016-04-20 23:23
  • 1397
    个人资料
    • 访问:185403次
    • 积分:5074
    • 等级:
    • 排名:第6398名
    • 原创:319篇
    • 转载:1篇
    • 译文:0篇
    • 评论:45条
    博客专栏
    最新评论