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.

题目

此题是[leetcode]200. Number of Islands岛屿个数小岛题的变种

要数出不同小岛的个数,题意定义了不同小岛: 互相不能通过非反射、旋转而转化

思路

- In 2D array, once we find 1st '1',  we find an island.  Meanwhile, we set a 'draw_begin' , ready to draw such island's shape

- DFS to loop through 4 different directions,  checking whether another '1' is connected with.  Meanwhile, we use u, d, l, r to draw island's shape.

- Once finish DFS, we set a 'draw_end',  indicating such island's drawing is done

- Why we use 'draw_begin' and  'draw_end' to set a bound ?

-  要去handle 类似以下这种情况

11     and     1
1 1 1

- Visited spots won't be visited again because they are updated to '0'

代码

 class Solution {
public int numDistinctIslands(int[][] grid) {
//put different drawing shape represented by string to hashset
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, i, j, sb, "draw_begin"); // set a bound
set.add(sb.toString());
}
}
}
// how many diffent shapes represented by string in hashset
return set.size();
}
private void dfs(int[][] grid, int i, int j, StringBuilder sb, String dir) {
// out of bound or not an island
if(i < 0 || i == grid.length || j < 0 || j == grid[i].length
|| grid[i][j] == 0) return; sb.append(dir);
grid[i][j] = 0;// mark a visted spot
dfs(grid, i-1, j, sb, "u");// up
dfs(grid, i+1, j, sb, "d");// down
dfs(grid, i, j-1, sb, "l");// left
dfs(grid, i, j+1, sb, "r");// right
sb.append("draw_end"); // set a bound
}
}

[leetcode]694. Number of Distinct Islands你究竟有几个异小岛?的更多相关文章

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

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

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

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

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

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

  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. Shell编程:小白初步

    shell类型: shell的历史网络上有一大堆,这里就不介绍了.但是我们的Linux系统上是有许多种shell的我们可以查看:使用命令 vi /etc/passwd 可以查看用户对应的shell(其 ...

  2. 在系统中使用Bean Validation验证参数

    转自:http://www.importnew.com/18561.html 为什么要使用Bean Validation?  当我们实现某个接口时,都需要对入参数进行校验.例如下面的代码 1 2 3 ...

  3. JavaScript 从定义到执行,你应该知道的那些事

    JavaScript从定义到执行,JS引擎在实现层做了很多初始化工作,因此在学习JS引擎工作机制之前,我们需要引入几个相关的概念:执行环境栈.执行环境.全局对象.变量对象.活动对象.作用域和作用域链等 ...

  4. 16.0 Auth0注册与设置

    首先呢?注册https://manage.auth0.com 填写回调网页,意思是当我们点sign in 那个按钮的时候 会访问这个官网 这个官网又回调下面的网页,不然会报错.这个网站因为我们是开发所 ...

  5. 小程序:pages/index/index/出现脚本错误或未正确调用Page()

    第一次尝试玩小程序,配置好以后报错.pages/index/index/出现脚本错误或未正确调用Page() 才发现解决的方式是:新建的文件默认是没有page()的,所以你需要在.js文件中加上Pag ...

  6. 抢红包js程序

    https://www.cnblogs.com/miid/p/5192235.html <!DOCTYPE html> <html> <head> <meta ...

  7. python闭包的代码

  8. 关于git经常忘记的:远程仓库关联。

    我们有时习惯建立好工程后再传到git上,这是时候就忘记咋弄啦, 其实,只要配置远程仓库就行: git remote add +url...具体看网上哦,这里提醒下 Git clone远程分支 Git ...

  9. 初始C语言中的数组(男神翁凯老师MOOC)

    定义数组 ●<类型>变量名称[元素数量]; ● int grades[100]; ●double weight[20]; ●元素数量必须是整数 ●C99之前:元素数量必须是编译时刻确定的字 ...

  10. workerman 平滑重启

    <?phpuse Workerman\Worker;use Workerman\Lib\Timer; require_once '../../web/Workerman/Autoloader.p ...