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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么存一块岛屿的形状:其实就是存方向构成的string就行了
每次走过一个点,都要设置该点为0,因为形状只统计一次,不重复
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
DFS就是:走到底-到最后一行改变条件-没出界就再走到底,反正就是走到底就对了
{1, 1, 0},
{0, 1, 1},
{0, 0, 0},
{1, 1, 1},
{0, 1, 0}
为了分辨方向序列,加星号的位置不同。
curShape = rdr
curShape = rdr*
curShape = rdr**
curShape = rdr***
curShape = rd
curShape = rd*r
curShape = rd*r*
curShape = rd*r**
[一刷]:
- stringbuilder需要专门的append函数而不是加号来连接string,所以在dfs中的参数就是它本身
[二刷]:
- if grid[i][j] = 1后从主函数进去就行,dfs不用再写一次了
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(mn) Space complexity: O(mn)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int numDistinctIslands(int[][] grid) {
//corner cases
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0; //initialization: StringBuilder, hashset
StringBuilder curShape;
Set<String> set = new HashSet<String>(); //start to expand if grid[i][j] == 1
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
curShape = new StringBuilder(); expandIslands(grid, i, j, curShape, "");
//add to set after trimed
set.add(curShape.toString().trim());
}
}
} //return keyset
return set.size();
} public void expandIslands(int[][] grid, int x, int y, StringBuilder curShape, String directions) {
//exit case
if (grid[x][y] == 0 || x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) return ; //set grid[x][y] = 0
grid[x][y] = 0;
//append the cur directions
curShape.append(directins); //expand in 4 directions
expandIslands(grid, x - 1, y, curShape, "U");
expandIslands(grid, x + 1, y, curShape, "D");
expandIslands(grid, x, y + 1, curShape, "R");
expandIslands(grid, x, y - 1, curShape, "L"); //add a * if neccessary
curShape.append(" "); }
}
694. Number of Distinct Islands 形状不同的岛屿数量的更多相关文章
- 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 不同岛屿的个数
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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- [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] 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] 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] 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 ...
随机推荐
- vim粘贴缩进问题
vim不支持直接从其他应用复制内容粘贴过来,而是模拟用户键盘输入来实现的,一般设置vim在换行时自动以上一行的的缩进为初始位置,这样就会导致复制过来的内容出现缩进错乱. set paste 解决粘贴乱 ...
- 光速搭lvs + keepalived + nginx
环境: VIP 192.168.2.224 LVS 192.168.2.217 centos7 nginx1 192.168.2.231 cen ...
- Hadoop与MPP是什么关系?有什么区别和联系?
HADOOP与MPP是什么关系?有什么区别和联系? 适用范围.应用领域分别是什么? 其实MPP架构的关系型数据库与Hadoop的理论基础是极其相似的,都是将运算分布到节点中独立运算后进行结果合并.个人 ...
- Excel技巧--按内容分列与合并
上表的A列,如果想要按横线分隔开多列,复制粘贴很麻烦,可以使用“数据”-->“分列”来分隔开: 1.选择A列,在A列后预先插入三列空列.点击“数据”—>“分列”,对话框选择按“分隔符号”分 ...
- 黄聪:JS数学计算精度修正
问题描述 如果我问你,4330.61乘以100等于多少,我猜你肯定跟我说:“肯定是 433061”啊! 是啊,要我我也是这么回答,来来来我们来看看浏览器怎么说吧,如下图 浏览器告诉我,他就是算不对 ...
- 黄聪:用 CSS 实现元素垂直居中,有哪些好的方案?
1.不知道自己高度和父容器高度的情况下, 利用绝对定位只需要以下三行: parentElement{ position:relative; } childElement{ position: abso ...
- Java中final关键字修饰变量、方法、类的含义是什么
Java中的关键字final修饰变量.方法.类分别表示什么含义? 先看一个简单的介绍 修饰对象 解释说明 备注 类 无子类,不可以被继承,更不可能被重写. final类中的方法默认是final的 方法 ...
- 理解Linux系统负荷load average
理解Linux系统负荷 一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在 ...
- nginx的proxy_pass路径转发规则最后带/问题
一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...
- redis消息通知(任务队列/优先级队列/发布订阅模式)
1.任务队列 对于发送邮件或者是复杂计算这样的操作,常常需要比较长的时间,为了不影响web应用的正常使用,避免页面显示被阻塞,常常会将此类任务存入任务队列交由专门的进程去处理. 队列最基础的方法如下: ...