Description

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.

Problem URL


Solution

给一二维数组表示1为岛屿,找到不同形状的岛屿,旋转相同也不可以的。是一个典型的dfs题目,双for循环遍历,如果是1(很关键)则开始dfs操作,在dfs中,如果越界或者不为1或者visited就返回,然后对四个方向进行dfs操作,最后设置对正常的返回值(回退情况)。模版题。

Using a string to denote the shape of an island. If the island have same shape, the dfs traversal order would be same order. Remember add ‘b’ for back track.

Code

class Solution {public int numDistinctIslands(int[][] grid) {if (grid == null || grid.length == 0 || grid[0].length == 0){return 0;}Set<String> set = new HashSet<>();for (int i = 0; i < grid.length; i++){for (int j = 0; j < grid[i].length; j++){if (grid[i][j] != 0){StringBuilder sb = new StringBuilder();dfs(grid, sb, i, j, 'o');set.add(sb.toString());  }}}return set.size();}private void dfs(int[][] grid, StringBuilder sb, int i, int j, char dir){if (i < 0 || i >= grid.length || j < 0 || j >= grid[i].length || grid[i][j] == 0){return;}sb.append(dir);grid[i][j] = 0;dfs(grid, sb, i + 1, j, 'd');dfs(grid, sb, i - 1, j, 'u');dfs(grid, sb, i, j + 1, 'r');dfs(grid, sb, i, j - 1, 'l');sb.append('b');}
}

Time Complexity: O(mn)
Space Complexity: O(mn)


Review

694. Number of Distinct Islands相关推荐

  1. pandas使用groupby函数和agg函数获取每个分组特定变量独特值的个数(number of distinct values in each group in dataframe)

    pandas使用groupby函数和agg函数获取每个分组特定变量独特值的个数(number of distinct values in each group in dataframe) 目录

  2. |spoj 694|后缀数组|Distinct Substrings

    spoj 694 给出一个字符串,求字符串中不相同的子串个数. 我们可以知道,字符串中的每个子串都是某个后缀的前缀,于是题目转化为求不相同的后缀的前缀问题.对于每一个SA[k]SA[k]开始的后缀,将 ...

  3. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  4. Leetcode总结(Island专题)

    Leetcode总结之 Island专题 Table of Contents 总结 200. Number of Islands 305. Number of Islands II 695. Max ...

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

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  6. LinkedIn TAG

    1 [leetcode]243. Shortest Word Distance最短单词距离 Two Pointers 2 [leetcode]244. Shortest Word Distance I ...

  7. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  8. leetcode岛屿类问题

    岛屿类问题 问题基础 如何在二维矩阵中使用 DFS 搜索呢?如果你把二维矩阵中的每一个位置看做一个节点,这个节点的上下左右四个位置就是相邻节点,那么整个矩阵就可以抽象成一幅网状的「图」结构. 根据二叉 ...

  9. LeetCode:115. Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A ...

最新文章

  1. 后台的代理nginx部署方法
  2. 整理一周的Python资料,包含各阶段所需网站、项目,2020燥起来!
  3. 关于大数据的那些事儿
  4. html页面渲染vue组件,Vue组件页面渲染的基本流程
  5. domino缺省注册服务器或无法访问,Domino服务器挂起时的现象
  6. 平果手机桌面计算机,苹果手机便签记事本怎么在Windows电脑桌面上使用?
  7. jedate时间插件
  8. 【JavaScript】运算符及其优先级
  9. PyQt5 与PyQt4的区别
  10. Science观点:不同细菌物种间极少合作—合理利用细菌间普遍存在的竞争关系来替代抗生素...
  11. Scratch中的变量
  12. Git 各指令的本质,真的是通俗易懂!
  13. java springboot mybaits 邮箱注册实现
  14. CCF-CSP认证知识要求
  15. Java 安全编程详解
  16. 关于VC中删除非空文件夹
  17. SpringBoot-多模块打包与问题排解
  18. 源码/反码/补码 (转)
  19. JST-IOT-TPN三相宽带载波模块在物联网中的应用
  20. php注册路由,thinkphp动态注册路由

热门文章

  1. 以太网巨人3Com退出历史舞台
  2. ALtera DE2开发板学习01
  3. 小白推荐系统扫盲记——数据分析
  4. KPA EtherCAT主站协议栈基准
  5. 免费电子书下载 网站(11个)
  6. 收集得最全的sql 语句
  7. WPF之触发器Triggers
  8. Linux 一句精彩的回答【转】
  9. freeswitch实战八(动态生成拨号计划)
  10. Prophet文档中文翻译--multiplicative_seasonality