417. Pacific Atlantic Water Flow
正常做的,用了645MS。。感觉DFS的时候剪枝有问题。。
为了剪枝可能需要标记一个点的4种情况:
1:滨临大西洋,所有太平洋来的点可以通过;
2:濒临太平洋,所有大西洋来的点可以通过;
3:都不濒临,直接RETURN;
4:都濒临,通过;
这样的话用DP记录情况,应该能做。。
看了答案发现一开始思路就错了。。但是这个645MS居然能AC。。。。。。。。。。 正确思路看第二段代码吧。
public class Solution
{
public List<int[]> pacificAtlantic(int[][] matrix)
{
List<int[]> res = new ArrayList<int[]>();
if(matrix.length == 0) return res;
boolean[][] dp = new boolean[matrix.length][matrix[0].length];
for(int i = 0; i < matrix.length;i++)
for(int j = 0; j < matrix[0].length;j++)
{
boolean[] a = new boolean[2];
boolean[][] visited = new boolean[matrix.length][matrix[0].length];
helper(matrix,dp,visited,i,j,a);
if(a[0] && a[1])
{
int[] temp = new int[2];
temp[0] = i;
temp[1] = j;
dp[i][j] = true;
res.add(temp);
}
}
return res;
}
public void helper(int[][] map,boolean[][] dp,boolean[][] visited, int m,int n,boolean[] a)
{
int val = map[m][n];
if(dp[m][n])
{
a[0]=true;
a[1]=true;
return;
}
if(m == 0) a[0] = true;
else if(map[m-1][n] <= val && !visited[m-1][n])
{
visited[m-1][n] = true;
helper(map,dp,visited,m-1,n,a);
}
if(a[0]&&a[1]) return;
if(n == 0) a[0] = true;
else if(map[m][n-1] <= val && !visited[m][n-1])
{
visited[m][n-1] = true;
helper(map,dp,visited,m,n-1,a);
}
if(a[0]&&a[1]) return;
if(m == map.length-1) a[1] = true;
else if(map[m+1][n] <= val && !visited[m+1][n])
{
visited[m+1][n] = true;
helper(map,dp,visited,m+1,n,a);
}
if(a[0]&&a[1]) return;
if(n == map[0].length-1) a[1] = true;
else if(map[m][n+1] <= val && !visited[m][n+1])
{
visited[m][n+1] = true;
helper(map,dp,visited,m,n+1,a);
}
return;
}
}
看了讨论,发现自己好蠢,这个水平别去面试了,回家吃屎行了。

不一定需要遍历整个图。以滨临大西洋的做DFS,再以滨临太平洋的做DFS,合集就是要求的点。
public class Solution
{
public List<int[]> pacificAtlantic(int[][] matrix)
{
List<int[]> res = new ArrayList<int[]>();
if(matrix.length == 0) return res;
int m = matrix.length;
int n = matrix[0].length;
boolean[][] p = new boolean[m][n];
boolean[][] a = new boolean[m][n];
boolean[][] visited = new boolean[m][n];
for(int i = 0; i < matrix.length;i++)
{
p[i][0] = true;
a[i][n-1] = true;
visited = new boolean[m][n];
helper(matrix,p,i,0,visited);
visited = new boolean[m][n];
helper(matrix,a,i,n-1,visited);
}
for(int i = 0; i < matrix[0].length;i++)
{
p[0][i] = true;
a[m-1][i] = true;
visited = new boolean[m][n];
helper(matrix,p,0,i,visited);
visited = new boolean[m][n];
helper(matrix,a,m-1,i,visited);
}
for(int i = 0; i < m;i++)
for(int j = 0; j < n;j++)
if(a[i][j] == true && p[i][j] == true)
{
int[] temp = new int[2];
temp[0] = i;
temp[1] = j;
res.add(temp);
}
return res;
}
public void helper(int[][] matrix,boolean[][] map,int m, int n,boolean[][] visited)
{
if(visited[m][n]) return;
visited[m][n] = true;
map[m][n] = true;
int v = matrix[m][n];
if(m > 0 && matrix[m-1][n]>=v) helper(matrix,map,m-1,n,visited);
if(n > 0 && matrix[m][n-1]>=v) helper(matrix,map,m,n-1,visited);
if(m < map.length-1 && matrix[m+1][n] >= v) helper(matrix,map,m+1,n,visited);
if(n < map[0].length-1 && matrix[m][n+1] >= v) helper(matrix,map,m,n+1,visited);
}
}
417. Pacific Atlantic Water Flow的更多相关文章
- [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
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...
- 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- [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] Pacific Atlantic Water Flow 题解
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- 一段C++代码想到的问题
今天在学习<Unix环境高级编程>,第七章进程环境给出了一个进程的内存分布示意图,从下往上依次为“正文段->初始化数据->未初始化数据(默认初始化为0)->堆(从低地址到 ...
- ARM开发板系统移植-----kernel的编译
前面一篇文章http://www.cnblogs.com/linzizhang/p/4817336.html介绍了开发板上系统软件的第一部分--bootloader的编译方法. 背景:把bootloa ...
- Android 中LinearLayout控件属性
id 为控件指定相应的ID text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符 g ...
- 怎样让老浏览器兼容html5新标签
CSS样式设置默认样式: <style> article, aside, canvas, details, figcaption, figure, footer, header, hgro ...
- 一些static_cast const_cast
static_cast:干杂活的,那三个都有各自的专有用途,那三个不做的都由这个转型符来做,只要它能做的,用C语法的强制类型转换运算符也一定能够完成:但话又说回来了,C强制类型转换能做的,它可不一定都 ...
- Python资源汇集
Python资源汇集 一 实用教程 廖雪峰网站 第一,Python教程:提供了循序渐进,重点是可操作的实用教程. 第二,Web App 项目教程.给出一个用16天完成的Python Web APP项目 ...
- typedef 类型重命名 和 #define 宏定义(1)
http://www.blogjava.net/jasmine214--love/archive/2010/11/29/339307.html 在现实生活中,信息的概念可能是长度,数量和面积等.在C语 ...
- 2016022611 - redis订阅发布命令集合
redis消息订阅发布命令 参考地址:http://www.yiibai.com/redis/redis_pub_sub.html 消息发送者发送消息,通过redis的channal,消息接收者获取消 ...
- 一种轻量的openresty路由设计
在使用openresty开发接口的过程会发现一个问题,那就是接口的地址问题怎么解决,最好一个接口地址对应一个lua文件,也可以在nginx.conf 配置中使用content_by_lua 来编写接口 ...
- iOS 细节 问题
1.当一个空指针(nil pointer)调用了一个方法会发生什么? 安然无恙 —— 这是oc自带的消息机制,nil也能发送消息,而不会报错 2.为什么retainCount绝对不能用在发布的代码中? ...