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总的的边数减去重叠的 ...
随机推荐
- [实战]MVC5+EF6+MySql企业网盘实战(11)——新建文件夹2
写在前面 上篇文章实现了创建文件夹的功能,这里面将实现单击文件夹,加载列表的功能. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网 ...
- jquery 验证
引入JS <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src=&qu ...
- Redis实战(七)
修改数据 C#语言修改Redis示例. 1.通过key修改单个value using (var redisClient = RedisManager.GetClient()) { var user = ...
- 将 sublime test3 添加到鼠标右键的方法
将 sublime test3 添加到鼠标右键的方法 将 sublime_addright.inf 文件放到 sublime text 安装目录进行安装即可. 文件名:sublime_addright ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1
题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...
- 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 F - 打铁的箱子
题目描述 作为彩虹岛上最擅长打铁的人,
- 小数据池,bytes
'''python2 python3 '''#python2#print() print 'abc'#range() xrange() 生成器# raw_input() #python3#print( ...
- C和指针之学习笔记(5)
第10章 使用结构和指针 单链表 typedef struct NODE { struct NODE *link; int value; } Node; 插入到一个有序单链表: #include< ...
- LCA(最近公共祖先)--tarjan离线算法 hdu 2586
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 5298 Solid Geometry Homework 暴力
Solid Geometry Homework 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5298 Description Yellowstar ...