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:

  1. 11000
  2. 11000
  3. 00011
  4. 00011

Given the above grid map, return 1.

Example 2:

  1. 11011
  2. 10000
  3. 00001
  4. 11011

Given the above grid map, return 3.

Notice that:

  1. 11
  2. 1

and

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

这道题让我们求不同岛屿的个数,是之前那道 Number of Islands 的拓展,难点是如何去判断两个岛屿是否是不同的岛屿,首先1的个数肯定是要相同,但是1的个数相同不能保证一定是相同的岛屿,比如例子2中的那两个岛屿的就不相同,就是说两个相同的岛屿通过平移可以完全重合,但是不能旋转。如何来判断呢,可以发现可以通过相对位置坐标来判断,比如使用岛屿的最左上角的1当作基点,那么基点左边的点就是 (0,-1),右边的点就是 (0,1), 上边的点就是 (-1,0),下面的点就是 (1,0)。则例子1中的两个岛屿都可以表示为 [(0,0), (0,1), (1,0), (1,1)],点的顺序是基点-右边点-下边点-右下点。通过这样就可以判断两个岛屿是否相同了,下面这种解法没有用数组来存,而是 encode 成了字符串,比如这四个点的数组就存为 "0_0_0_1_1_0_1_1_",然后把字符串存入集合 unordered_set 中,利用其自动去重复的特性,就可以得到不同的岛屿的数量啦,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. vector<vector<int>> dirs{{,-},{-,},{,},{,}};
  4. int numDistinctIslands(vector<vector<int>>& grid) {
  5. int m = grid.size(), n = grid[].size();
  6. unordered_set<string> res;
  7. vector<vector<bool>> visited(m, vector<bool>(n, false));
  8. for (int i = ; i < m; ++i) {
  9. for (int j = ; j < n; ++j) {
  10. if (grid[i][j] == && !visited[i][j]) {
  11. set<string> s;
  12. helper(grid, i, j, i, j, visited, s);
  13. string t = "";
  14. for (auto str : s) t += str + "_";
  15. res.insert(t);
  16. }
  17. }
  18. }
  19. return res.size();
  20. }
  21. void helper(vector<vector<int>>& grid, int x0, int y0, int i, int j, vector<vector<bool>>& visited, set<string>& s) {
  22. int m = grid.size(), n = grid[].size();
  23. visited[i][j] = true;
  24. for (auto dir : dirs) {
  25. int x = i + dir[], y = j + dir[];
  26. if (x < || x >= m || y < || y >= n || grid[x][y] == || visited[x][y]) continue;
  27. string str = to_string(x - x0) + "_" + to_string(y - y0);
  28. s.insert(str);
  29. helper(grid, x0, y0, x, y, visited, s);
  30. }
  31. }
  32. };

当然我们也可以不 encode 字符串,直接将相对坐标存入数组中,然后把整个数组放到集合 set 中,还是会去掉相同的数组,而且这种解法直接在 grid 数组上标记访问过的位置,写起来更加简洁了,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. vector<vector<int>> dirs{{,-},{-,},{,},{,}};
  4. int numDistinctIslands(vector<vector<int>>& grid) {
  5. int m = grid.size(), n = grid[].size();
  6. set<vector<pair<int, int>>> res;
  7. for (int i = ; i < m; ++i) {
  8. for (int j = ; j < n; ++j) {
  9. if (grid[i][j] != ) continue;
  10. vector<pair<int, int>> v;
  11. helper(grid, i, j, i, j, v);
  12. res.insert(v);
  13. }
  14. }
  15. return res.size();
  16. }
  17. void helper(vector<vector<int>>& grid, int x0, int y0, int i, int j, vector<pair<int, int>>& v) {
  18. int m = grid.size(), n = grid[].size();
  19. if (i < || i >= m || j < || j >= n || grid[i][j] <= ) return;
  20. grid[i][j] *= -;
  21. v.push_back({i - x0, j - y0});
  22. for (auto dir : dirs) {
  23. helper(grid, x0, y0, i + dir[], j + dir[], v);
  24. }
  25. }
  26. };

既然递归 DFS 可以,那么迭代的 BFS 就坐不住了,其实思路没什么区别,这种类似迷宫遍历的题都是一个套路,整体框架都很像,细枝末节需要改改就行了,参见代码如下:

解法三:

  1. class Solution {
  2. public:
  3. vector<vector<int>> dirs{{,-},{-,},{,},{,}};
  4. int numDistinctIslands(vector<vector<int>>& grid) {
  5. int m = grid.size(), n = grid[].size();
  6. set<vector<pair<int, int>>> res;
  7. for (int i = ; i < m; ++i) {
  8. for (int j = ; j < n; ++j) {
  9. if (grid[i][j] != ) continue;
  10. vector<pair<int, int>> v;
  11. queue<pair<int, int>> q{{{i, j}}};
  12. grid[i][j] *= -;
  13. while (!q.empty()) {
  14. auto t = q.front(); q.pop();
  15. for (auto dir : dirs) {
  16. int x = t.first + dir[], y = t.second + dir[];
  17. if (x < || x >= m || y < || y >= n || grid[x][y] <= ) continue;
  18. q.push({x, y});
  19. grid[x][y] *= -;
  20. v.push_back({x - i, y - j});
  21. }
  22. }
  23. res.insert(v);
  24. }
  25. }
  26. return res.size();
  27. }
  28. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/694

类似题目:

Number of Islands

Number of Distinct Islands II

参考资料:

https://leetcode.com/problems/number-of-distinct-islands/

https://leetcode.com/problems/number-of-distinct-islands/discuss/150037/DFS-with-Explanations

https://leetcode.com/problems/number-of-distinct-islands/discuss/108474/JavaC%2B%2B-Clean-Code

https://leetcode.com/problems/number-of-distinct-islands/discuss/108475/Java-very-Elegant-and-concise-DFS-Solution(Beats-100)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 694. Number of Distinct Islands 不同岛屿的个数的更多相关文章

  1. [LeetCode] 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) conn ...

  2. [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) conn ...

  3. [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) conn ...

  4. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  6. [LeetCode] 711. 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) conn ...

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

  8. 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  9. [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) conn ...

随机推荐

  1. 《一起学mysql》3

    索引和查询优化   为什么要索引? 想想我们上小学的时候是怎么查字典的,比方查 理想的 “理”,首先在索引里找到声母 “l”,再找到 “li” 找到 “li”所在的页数,   我们之前建的所有mysq ...

  2. 安装pip-9.0.1-py2.py3-none-any.whl

    pip的安装 1.从https://pypi.python.org/pypi/pip#downloads下载所需的.whl文件 2.将下载的文件放入Python的根目录 我的根目录是F:\Python ...

  3. 修改Hexo自动生成的HTML文件名

    导读 我们在使用Hexo框架生成静态博客时,其实是将你写好的.md文件输出成HTML文件进行渲染,其中HTML的文件名称就是.md的文件名称. 而我们为了编辑文章方便,为了通过文件名就知道这是哪篇文章 ...

  4. Spring Cloud Feign 服务消费调用(三)

    序言 Spring Cloud Netflix的微服务都是以HTTP接口的形式暴露的,所以可以用Apache的HttpClient或Spring的RestTemplate去调用 而Feign是一个使用 ...

  5. 百度编辑器上传视频报Http请求错误,.net实现方式

    在使用百度编辑器上传视频的时候遇到一个很奇怪的问题,当视频大小在20M以下的时候,上传正常.当大于20M时,一直报Http请求错误. 处理步骤: 1.修改编辑器配置信息,如图所示,改成你想要的大小 2 ...

  6. ASP.NET MVC 中枚举生成下拉框

    最近公司在开发财务系统,在工作中遇到不少的地方需要下拉框. 但是枚举框中数据的内容又来自枚举. 枚举代码如下: public class EnumDemo { public enum Value { ...

  7. 【UWP】实现 FindAncestor 绑定

    在 WPF 里,我们是可以在 RelativeSource 上面实现的,举个例子: <Grid Tag="2"> <Button> <Grid Tag ...

  8. npm和cnpm命令后无响应

    问题: 1.把前端环境配制完毕之后,打开项目,输入cnpm install之后,光标一直在另起一行的位置闪,但是丝毫没有在安装的迹象. 2.打开cmd,在窗体中输入node -v 可以显示版本,但是输 ...

  9. 关于创建node服务

    1.环境条件准备: A.确定node已经创建 B.npm或cnpm已经下载,npm和cnpm其实是一个道理 C.mysql或者使用其他数据库已经安装(本例使用mysql) 2.开始创建,首先新建一个文 ...

  10. uni-app学习(五)好用的插件3

    1. uni-app学习(五)好用的插件3 1.1. 分享推广页面 分享推广页面,分享第三方.保存二维码.复制推广地址 模板地址 示例 这个用到的几率还是蛮大的,可以直接拿来修改下用 1.2. 教育A ...