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总的的边数减去重叠的 ...
随机推荐
- git客户端基本操作
首先下载git 一路next安装好了之后,打开任意盘符,右键打开git bash here 首先:初始首次的用户名和邮箱,之后就不用了. git config --global user.name & ...
- 主流PHP框架性能评测 (引用)
主要涉及到的框架有 CodeIgniter 老品牌易用性框架yaf 鸟哥用c写的php扩展,高性能框架yii 自动生成代码(gii)laravel 号称最优雅的框架swoole framework 支 ...
- 表A中一条记录的两个字段都对应于表B的同一个字段 如何查询?SQL, thinkphp[5]
表 A=approval_order, B=admin, 表A中technician_username, salesman_username 都是id号,中文名保存在admin表的nickname ...
- Java变量和运算符
1.变量 变量概述 什么是变量?变量是一个内存中的小盒子(小容器),容器是什么?生活中也有很多容器,例如水杯是容器,用来装载水:你家里的大衣柜是容器,用来装载衣裤:饭盒是容器,用来装载饭菜.那么变量是 ...
- React Native 系列(八)
前言 本系列是基于React Native版本号0.44.3写的.我们都知道,一个App不可能只有一个不变的界面,而是通过多个界面间的跳转来呈现不同的内容.那么这篇文章将介绍RN中的导航. 导航 什么 ...
- Redis学习篇(二)之Hash类型及其操作
HSET 作用: 将哈希表key中的域field设置成指定的value 语法:HSET key field value HSET userinfo name 'zhangsan' HSET useri ...
- 「SCOI2015」情报传递
「SCOI2015」情报传递 题目描述 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有 \(n\) 名情报员.每名情报员可能有若干名(可能没有)下线,除 \(1\) 名大头目外其余 ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- [HAOI2012]外星人
题目大意: 告诉你一个数n,求满足φ^x(n)=1的x. 思路: 首先我们可以发现满足φ(n)=1的数只有2,也就是说你得到最终的结果,最后一步肯定是φ(2). 同时,可以发现φ(φ(2^k))=φ( ...
- 求n的阶乘 (python实现)
描述 给定一个数n,范围为0≤n≤100,请你编程精确的求出n的阶乘n!. 输入 输入数据有多行,每行一个整数n,当n<0时输入结束. 输出 输出n的阶乘. 样例输入 1234-1 样例输出 1 ...