[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) 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‘

代码

 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 }

原文地址:https://www.cnblogs.com/liuliu5151/p/9825367.html

时间: 2024-11-01 22:43:24

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

[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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

694. Number of Distinct Islands - Medium

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 island

[LC] 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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

[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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

[LeetCode] 200. Number of Islands 岛屿的数量

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

LeetCode | 0200. Number of Islands岛屿数量【Python】

LeetCode 0200. Number of Islands岛屿数量[Medium][Python][DFS] Problem LeetCode Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or v

LeetCode之“动态规划”:Distinct Subsequences

题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing th

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他