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 ...
随机推荐
- DBA 培训相应内容笔记
014-12-23 slow hang lock 三种常见性能问题 hang;整个数据库不响应,无法创建新连接. hanganalyze输出. dump文件是否有问题信息, 文件是否有其他信息 工具: ...
- TP缓存设计方案解析
TP的缓存主要依赖Cache类,Cache类其实是一个代理类,Cache类通过getInstance静态方法来获取缓存实例,而getInstance方式实际是调用Cache类的connect方法,该方 ...
- 速卖通api--发起授权
<? $reqURL_onLine = "https://gw.api.alibaba.com/openapi/http/1/system.oauth2/getToken/494739 ...
- 解决GDB输出Qt内置类型的显示问题
自从GDB 7.0之后,就加入了Pretty-Printer的这个概念.简单理解就是他可以让你用Python写一串脚本,然后让gdb去读取这串脚本后,可以自由的输出由你想自己定义的格式.我们直接举个简 ...
- jquery中mouseout和mouseleave 事件的区别
今天用jQuery写了一个选项卡的效果,用mouseout事件控制了鼠标的移出,结果发现在移出时div会发生闪动,于是网上各种查资料觉得用mouseleave更合适一些,
- JavaScript学习心得(一)
一Javascript简介 JavaScript是一种面向对象.弱类型的脚本语言!面向对象编程语言(OOP)意味着你用的几乎所有变量都是对象,对象是一种特殊的变量类型,有自己的子变量(称为属性)及函数 ...
- phpcms v9栏目列表调用每一篇文章内容方法1
我们先来看下默认栏目调用的代码: 复制代码代码如下:{pc:content action="lists" catid="$catid" num="25 ...
- Oracle数据库常用函数
Oracle常用函数: 20.COS返回一个给定数字的余弦SQL> select cos(-3.1415927) from dual;COS(-3.1415927)--------------- ...
- 2016021801 - Java内存区域学习笔记
根据<深入理解java虚拟机>学习归纳整理学习笔记 程序计数器 用途:当前线程的字节码文件的行号指示器.(当前机场负责控制飞机降落的空管员:当前线程表示当前机场, 所执行的字节码等同于被等 ...
- MATLAB中的结构数组
MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...