pacific-atlantic-water-flow(不错)
https://leetcode.com/problems/pacific-atlantic-water-flow/ // 其实,这道题目可以参考前面的那道:Trapping Rain Water II
// 可以看我blog上面的解法:
// http://www.cnblogs.com/charlesblc/p/5920258.html // 注意Java set的操作,retainAll 交集,removeAll差集
// 开始set里面放的是int[],但是发现比较的时候,都被认为不一样,所以需要换成Block // 第一次提交还报了一个Runtime Error,因为没有考虑row或者column为0的情况 public class Solution { private class Block {
public int row;
public int col;
public int height;
public Block(int r, int c, int h) {
row = r;
col = c;
height = h;
} public int hashCode() {
return row + col + height;
} public boolean equals(Object obj) {
if (obj instanceof Block) {
Block bk = (Block)obj;
return bk.row == row && bk.col == col;
}
return false;
} } public List<int[]> pacificAtlantic(int[][] matrix) {
List<int[]> ret = new ArrayList<int[]>(); int r = matrix.length;
if (r == 0) {
return ret;
}
int c = matrix[0].length;
if (c == 0) {
return ret;
} Set<Block> pSt = new HashSet<Block>();
Set<Block> aSt = new HashSet<Block>(); Queue<Block> qe = new PriorityQueue<Block>(1,
new Comparator<Block>() {
@Override
public int compare(Block o1, Block o2) {
return o1.height - o2.height;
}
}
);
boolean[][] visited = new boolean[r][c];
int[][] dirs = {{-1,0}, {0, 1}, {1, 0}, {0, -1}}; for (int i=0; i<r; i++) {
Block bk = new Block(i, 0, matrix[i][0]);
pSt.add(bk);
//System.out.printf("add pset: %d, %d\n", tmp[0], tmp[1]);
qe.offer(bk);
visited[i][0] = true;
}
for (int i=0; i<c; i++) {
Block bk = new Block(0, i, matrix[0][i]);
pSt.add(bk);
//System.out.printf("add pset: %d, %d\n", tmp[0], tmp[1]);
qe.offer(bk);
visited[0][i] = true;
} while (!qe.isEmpty()) {
Block bk = qe.poll();
for (int i=0; i<4; i++) {
int nr = bk.row + dirs[i][0];
int nc = bk.col + dirs[i][1];
if (nr < 0 || nr >= r || nc < 0 || nc >= c || visited[nr][nc]) {
continue;
}
visited[nr][nc] = true;
if (matrix[nr][nc] >= bk.height) {
Block nbk = new Block(nr, nc, matrix[nr][nc]);
pSt.add(nbk);
//System.out.printf("add pset new: %d, %d\n", tmp[0], tmp[1]);
qe.offer(nbk);
}
}
} // 重新初始化,计算atlantic
qe.clear();
visited = new boolean[r][c]; for (int i=0; i<r; i++) {
Block bk = new Block(i, c-1, matrix[i][c-1]);
aSt.add(bk);
//System.out.printf("add aset: %d, %d\n", tmp[0], tmp[1]);
qe.offer(bk);
visited[i][c-1] = true;
}
for (int i=0; i<c; i++) {
Block bk = new Block(r-1, i, matrix[r-1][i]);
aSt.add(bk);
//System.out.printf("add aset: %d, %d\n", tmp[0], tmp[1]);
qe.offer(bk);
visited[r-1][i] = true;
} while (!qe.isEmpty()) {
Block bk = qe.poll();
for (int i=0; i<4; i++) {
int nr = bk.row + dirs[i][0];
int nc = bk.col + dirs[i][1];
if (nr < 0 || nr >= r || nc < 0 || nc >= c || visited[nr][nc]) {
continue;
}
visited[nr][nc] = true;
if (matrix[nr][nc] >= bk.height) {
Block nbk = new Block(nr, nc, matrix[nr][nc]);
aSt.add(nbk);
//System.out.printf("add aset new: %d, %d\n", tmp[0], tmp[1]);
qe.offer(nbk);
}
}
} //System.out.printf("pset length: %d\n", pSt.size());
//System.out.printf("aset length: %d\n", aSt.size());
pSt.retainAll(aSt);
System.out.printf("set length: %d\n", pSt.size()); Iterator itr = pSt.iterator();
while (itr.hasNext()) {
Block bk = (Block)itr.next();
int[] val = {bk.row, bk.col};
ret.add(val);
}
return ret;
} }
pacific-atlantic-water-flow(不错)的更多相关文章
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- Leetcode: Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- LeetCode 417. Pacific Atlantic Water Flow
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...
- [LeetCode] 417. Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...
- [LeetCode] Pacific Atlantic Water Flow 题解
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- 417. Pacific Atlantic Water Flow
正常做的,用了645MS..感觉DFS的时候剪枝有问题.. 为了剪枝可能需要标记一个点的4种情况: 1:滨临大西洋,所有太平洋来的点可以通过: 2:濒临太平洋,所有大西洋来的点可以通过: 3:都不濒临 ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- Python爬虫-urllib的基本用法
from urllib import response,request,parse,error from http import cookiejar if __name__ == '__main__' ...
- poj2362 Square(DFS)
题目链接 http://poj.org/problem?id=2362 题意 输入n根棍子的长度,求这n根棍子是否能组成一个正方形. 思路 假设能组成正方形,则正方形的周长为sum,sum/4为正方形 ...
- U3D 基础
千里之行,始于足下! 最先执行的方法是:1.(激活时的初始代码)Awake2.Start3.Update(FixUpdate,LateUpdate)4.渲染模块(OnGUI)5.再向后,就是卸载模块( ...
- 转:LNMP虚拟主机PHP沙盒绕过/命令执行(php exec命令被禁之后)
LNMP虚拟主机PHP沙盒绕过/命令执行 lnmp更新1.2版本,很多东西都升级了,很棒.不过还是发现一个BUG. LNMP是一款linux下nginx.php.mysql一键安装包. 下载:http ...
- RecyclerView悬浮标题
效果图: 1.顶部会悬浮章的部分 2.第二章上滑会推挤第一章 3.第二章下拉会带出第一章 4.并不是所有时候都有悬浮部分(为什么这条标红,因为市面上几乎所有的悬浮都是必须存在且在顶部,害 ...
- angular4 监听input框输入值的改变
angular中一般控件会有change事件,但是如果某些控件没有这个事件 我们如何监听值的变化呢? 对于双向绑定的值,当值改变后监听事件有如下写法: 1. 如果是ngModel可以用ngModelC ...
- 趴一趴京东的Ajax动态价格页面
AJAX,异步加载技术!!! 之前在网上看过很多朋友有一种疑问,为什么在看京东网页的源代码里面看不到价格或则折扣一类的数据,而在网页上正常显示却能看到?...之前我也没有想到是AJAX,因为我写写爬虫 ...
- 常见http错误码
一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 详细分解: 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 ...
- [HDU5965]扫雷
[HDU5965]扫雷 题目大意: 一个\(3\times n(n\le10000)\)的扫雷,第\(2\)排没有雷.告诉你第\(2\)排上的数,问有几种埋雷的方案? 思路: 动态规划. 将\(1,3 ...
- bzoj 1211: [HNOI2004]树的计数 -- purfer序列
1211: [HNOI2004]树的计数 Time Limit: 10 Sec Memory Limit: 162 MB Description 一个有n个结点的树,设它的结点分别为v1, v2, ...