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的更多相关文章

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

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

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

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

  8. 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 用了第一种方式, ...

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

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

随机推荐

  1. Android 音视频深入 四 录视频MP4(附源码下载)

    本篇项目地址,名字是<录音视频(有的播放器不能放,而且没有时长显示)>,求star https://github.com/979451341/Audio-and-video-learnin ...

  2. Linux运维工程师需要掌握什么才能胜任工作呢

    万丈高楼平地起,所有一切的高深的技术都离不开最基本的技术,那么作为运维工程师的你,什么是最基本的技术呢,毫无疑问是Linux,Linux 是你所有一切技术的根源,试想一下如果你连基础的操作命令都不知道 ...

  3. Suffix树,后缀树

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. SQL-16 统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。

    题目描述 统计出当前各个title类型对应的员工当前薪水对应的平均工资.结果给出title以及平均工资avg.CREATE TABLE `salaries` (`emp_no` int(11) NOT ...

  5. SharePoint Framework 企业向导(六)

    博客地址:http://blog.csdn.net/FoxDave 接上一讲 部署SPFx解决方案 部署SPFx解决方案可以用两个步骤完成:1. 将脚本组件打成的包部署到一个CDN(内容分发网络) ...

  6. oracle语句录

    从表中选出一个某个单位最近的记录 select * from RSDL_SHXX where sbsj in (select max (sbsj) from RSDL_SHXX where DW_ID ...

  7. Final阶段第1周/共1周 Scrum立会报告+燃尽图 06

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2485] 版本控制:https://git.coding.net/liuyy08 ...

  8. shelly - HYMN TO INTELLECTUAL BEAUTY

    HYMN TO INTELLECTUAL BEAUTY III No voice from some sublimer world hath ever ⁠To sage or poet these r ...

  9. 转--让一个运行在SYSTEM权限下的进程与当前用户的桌面进行交互

    #define DESKTOP_ALL ( DESKTOP_READOBJECTS | DESKTOP_CreateWINDOW | \ DESKTOP_CreateMENU | DESKTOP_HO ...

  10. 关于“用VS2010的C++导入ADO导入不了,提示无法打开源文件msado15.tlh”的问题

    vc++2010中,要使用ado操作数据库,所以在stdafx.h中引入了ado的dll库,引入代码如下: #import "C:/Program Files/Common Files/Sy ...