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总的的边数减去重叠的 ...
随机推荐
- 开源IDS系列--snorby 进程正常,但是worker无法启动 The Snorby worker is not currently running
设置页面报错: The Snorby worker is not currently running. It's imperative you start the worker immediate ...
- Vugen 和controller 中的run-time setting区别
Vugen 和controller 中的run-time setting有什么不同?
- oracle chr(38) 和 ascii('&') 函数
oracle chr(38) 和 ascii('&') 函数 select chr(38) from dual; select ascii('&') from dual;
- Ubuntu 18.04 更改静态IP
2018年4月26日,ubuntu 18.04发行,第一时间下载安装了SERVER版本.使用VM14版本的虚拟机,开始使用DHCP获得IP地址,没有意外,可以直接上网.然而在更改VM的网络模式为桥接模 ...
- Python爬虫笔记(一)
个人笔记,仅适合个人使用(大部分摘抄自python修行路) 1.爬虫Response的内容 便是所要获取的页面内容,类型可能是HTML,Json(json数据处理链接)字符串,二进制数据(图片或者视频 ...
- Android中由Handler和内部类引起的内存泄漏
原文地址:http://johnnyshieh.github.io/android/2015/09/03/android-handler-memory-leak/ 在Android中我们经常用Hand ...
- STL容器 -- Vector
核心:Vector 是 STL 里的一个向量容器,可以像数组那样进行随机访问,能在尾部插入元素,对于元素的删除和插入可以动态管理内存. 头文件: #include <vector> 构造函 ...
- latex常用命令
首行不缩进:在顶格的段落前面加上 \noindent 增加空格:\vspace{长度}:精确增加垂直距离,\hspace{长度} 与 \addvspace{长度}:增加高度长度的垂直空间 在公式的上. ...
- Xamarin Android权限请求
Xamarin Android权限请求 Android权限规定了App是否可以访问特定的资源,如网络.电话和短信.在原有API 6.0之前,App在安全的时候,会请求一次权限.一旦安装后,App就 ...
- Selenium快速入门(上)
浏览器驱动下载 Edge浏览器 Firefox浏览器 Safari浏览器 Chrome浏览器 PhantomJS浏览器 下载完成之后,添加到环境变量. 声明浏览器对象 selenium支持的浏览器版本 ...