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) 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.
这道题让我们求不同岛屿的个数,是之前那道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中,利用其自动去重复的特性,就可以得到不同的岛屿的数量啦,参见代码如下:
- // "static void main" must be defined in a public class.
- public class Main {
- public static void main(String[] args) {
- //int[][] grid = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
- int[][] grid = {{1,1,0,1,1},{1,0,0,0,0},{0,0,0,0,1},{1,1,0,1,1}};
- System.out.println(numDistinctIslands(grid));
- }
- public static int numDistinctIslands(int[][] grid) {
- if(grid == null){
- return 0;
- }
- int row = grid.length;
- int column = grid[0].length;
- Set<String> set = new HashSet<>();
- for(int i=0; i<row; i++){
- for(int j=0; j<column; j++){
- if(grid[i][j] == 1){
- StringBuilder sb = new StringBuilder();
- Queue<int[]> queue = new LinkedList<>();
- queue.add(new int[]{i,j});
- grid[i][j] = 2;
- sb.append("0,0:");
- while(!queue.isEmpty()){
- int[] temp = queue.poll();
- int r = temp[0];
- int c = temp[1];
- //move up
- if(r - 1 >= 0 && grid[r - 1][c] == 1){
- int rr = r-1-i;
- int rc = c-j;
- queue.add(new int[]{r-1,c});
- sb.append(rr+","+rc+":");
- grid[r-1][c] = 2;
- }
- //move down
- if(r + 1 < row && grid[r + 1][c] == 1){
- int rr = r+1-i;
- int rc = c-j;
- queue.add(new int[]{r+1,c});
- sb.append(rr+","+rc+":");
- grid[r+1][c] = 2;
- }
- //move left
- if(c - 1 >= 0 && grid[r][c - 1] == 1){
- int rr = r-i;
- int rc = c-1-j;
- queue.add(new int[]{r,c-1});
- sb.append(rr+","+rc+":");
- grid[r][c-1] = 2;
- }
- //move right
- if(c + 1 < column && grid[r][c + 1] == 1){
- int rr = r-i;
- int rc = c+1-j;
- queue.add(new int[]{r,c+1});
- sb.append(rr+","+rc+":");
- grid[r][c+1] = 2;
- }
- }
- set.add(sb.toString());
- }
- }
- }
- return set.size();
- }
- }
LeetCode - Number of Distinct Islands的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 用了第一种方式, ...
- 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
随机推荐
- python全栈开发笔记-----------概要
Python开发 开发: 开发语言: 高级语言:python.Java.php .C# .Go .ruby . C++ .... ===>字节码 低级语言:C.汇编 ...
- docker samba
这个就是匿名用户可以登录访问,不能写. root登录,就可以写了. #命令,是在物理机上运行的. 主要是根据dockerfile构建镜像. 启动容器 进入镜像 设置root密码. 附smb.conf ...
- Web 开发最有用的50款 jQuery 插件集锦——《内容滑块篇》
http://www.cnblogs.com/lhb25/archive/2013/04/02/50-jquery-plugins-d.html responsive-carousel 是一个内容传送 ...
- java基础学习之final关键字
final可以修饰类.方法.变量,一旦使用了final则将不能改变被修饰的对象的引用; 被final修饰的类不可以被继承 被final修饰的方法不可以被覆盖 被final修饰的变量一般为常量,只允许对 ...
- Unity3D使用OpenFileDialog后崩溃
http://ask.unitymanual.com/question/24922 找了很久,原来是我的dll文件引错了,名字都一样,应该引用unity安装目录下的System.Window.Form
- springsecurity基于数据库验证用户
之前的springsecurity程序都是将数据存放在内存中的,通过 <security:user-service> <security:user name="user&q ...
- python flask 小项目
0 开始之前 网上看了很多教程,都不是很满意,因此自己写一个大型教程,从入门到做出一个比较完整的博客.此次教程不是直接把整个博客直接代码整理出来然后运行一遍就完事,我会从flask的各个模块讲起.所以 ...
- express框架以及配置项
以上命令会将 Express 框架安装在当前目录的 node_modules 目录中, node_modules 目录下会自动创建 express 目录.以下几个重要的模块是需要与 express 框 ...
- CRM工具简介
pacemaker是高可用集群中的CRM(Cluster Resource Manager)资源管理层,他是一个服务,可以作为一个单独的服务启动,不过在如果使用corosync1.4中,我们可以设置c ...
- java中高级
面试问题: 一.Java基础方面: 1.Java面相对象的思想的理解(主要是多态): http://blog.csdn.net/zhaojw_420/article/details/70477636 ...