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.

题目

此题是[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 类似以下这种情况

  1. 11 and 1
  2. 1 1 1

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

代码

  1. class Solution {
  2. public int numDistinctIslands(int[][] grid) {
  3. //put different drawing shape represented by string to hashset
  4. Set<String> set = new HashSet<>();
  5. for(int i = 0; i < grid.length; i++) {
  6. for(int j = 0; j < grid[i].length; j++) {
  7. if(grid[i][j] != 0) {
  8. StringBuilder sb = new StringBuilder();
  9. dfs(grid, i, j, sb, "draw_begin"); // set a bound
  10. set.add(sb.toString());
  11. }
  12. }
  13. }
  14. // how many diffent shapes represented by string in hashset
  15. return set.size();
  16. }
  17. private void dfs(int[][] grid, int i, int j, StringBuilder sb, String dir) {
  18. // out of bound or not an island
  19. if(i < 0 || i == grid.length || j < 0 || j == grid[i].length
  20. || grid[i][j] == 0) return;
  21.  
  22. sb.append(dir);
  23. grid[i][j] = 0;// mark a visted spot
  24. dfs(grid, i-1, j, sb, "u");// up
  25. dfs(grid, i+1, j, sb, "d");// down
  26. dfs(grid, i, j-1, sb, "l");// left
  27. dfs(grid, i, j+1, sb, "r");// right
  28. sb.append("draw_end"); // set a bound
  29. }
  30. }

[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. 使用ubuntu远程连接windows, Connect to a Windows PC from Ubuntu via Remote Desktop Connection

    from: https://www.digitalcitizen.life/connecting-windows-remote-desktop-ubuntu NOTE: This tutorial w ...

  2. RTS寻路相关资料收集

    http://www.cocoachina.com/game/20150824/13174.html RVO算法 RVO避开agent

  3. 健康检测文件httpchk.jsp

    静态显示: <html><body><center> Now time is: <%=new java.util.Date()%> </cente ...

  4. UE 不生成.bak文件

    .bak文件是UE处理文件时自动备份的文件,可以取消备份这样就不会生成.bak文件了 菜单:高级-设置-文件处理-备份        应用和确定

  5. C++ 设置透明背景图片

    背景:            有两个图片,一个是目标背景图片, 一个是带有自身背景色彩的彩色图片            先将这彩色图片绘制到目标背景图片中, 这一步通过BITBLT就可实现.   但实 ...

  6. 单例模式 demo

    // 用单例模式实现自定义颜色类 public class MyColor { private static MyColor _redColor = null; public static MyCol ...

  7. 将一个dropdownlist从一个div复制到另一个div

    <select id="dropdwon1"> <option value=">Item1</option> <option v ...

  8. /src/log4j.xml

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration S ...

  9. day22 面向对象基础

    1.什么是面向过程 在介绍面向对象之前,要先明确面向过程 在这之前我们所写的任何代码都是面向过程的 什么是面向过程? 是一种编程思想 面对 朝向 在编写代码时,要时刻想着过程这两个字 过程指的是什么? ...

  10. 830. Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...