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 ...
随机推荐
- linux命令sed学习笔记
sed其实就是两个主要的知识点,那就是“怎么选择”和“怎么操作”!
- EDM推送
一.需求描述: 日前,做了一个发送客户账单的功能,邮件模板采用自定义,生成vm文件,保存至redis, 采用jodd-mail发送邮件,查询用户账单数据,账单明细,缓存加载模板并渲 ...
- etTimeout与setInterval方法的区别
etTimeout与setInterval方法的区别 setTimeout()用于设定在指定的时间之后执行对应的函数或代码.,在全局作用域下执行 setTimeout(code,time[,args… ...
- ListView复用和优化详解
我们每一个Android开发人员对ListView的使用肯定是很熟悉的,然而多少人能真正的懂ListView的缓存机制呢,说白了就是ListView为了提高效率,而内部实现的一种优化,牺牲一点内存.而 ...
- WPF之 XAML集合项简单演示
我们直接通过xaml文件演示一个简单的xaml集合项: <Window x:Class="WPF_XAML集合项.MainWindow" xmlns="http:/ ...
- JsRender
JsRender 不少前端人员应该都用过,它是一个比较强大的模板,不牵涉太多技术依赖,使用起来非常舒服.我本人在前端开发中使用React之前,都是用的它了(实际上我感觉React没有JsViewes好 ...
- C#事件(Event)学习日记
event 关键字的来由,为了简化自定义方法的构建来为委托调用列表增加和删除方法. 在编译器处理 event 关键字的时候,它会自动提供注册和注销方法以及任何必要的委托类型成员变量. 这些委托成员变量 ...
- js代码判断浏览器种类IE、FF、Opera、Safari、chrome及版本
这篇文章主要分享了判断IE.FF.Opera.Safari.Chrome等浏览器和版本的两种方法,需要的朋友可以参考下 因为ie10-ie11的版本问题,不再支持document.all判断,所以ie ...
- [AHOI2015 Junior] [Vijos P1943] 上学路上 【容斥+组合数】
题目链接:Vijos - P1943 题目分析 这是 AHOI 普及组的题目,然而我并不会做= =弱到不行= = 首先,从 (x, 0) 到 (0, y) 的最短路,一定是只能向左走和向上走,那么用组 ...
- 如何用 React Native 创建一个iOS APP?(三)
前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...