417. Pacific Atlantic Water Flow
思路:构造两个二维数组分别存储大西洋和太平洋的结果,先初始化边界,然后从边界出发,深度优先遍历,标记满足条件的所有节点

static int[] dx = new int[]{-1,0,0,1};
static int[] dy = new int[]{0,1,-1,0};
public List<int[]> pacificAtlantic(int[][] matrix) {
List<int[]> res = new ArrayList<int[]>();
if(matrix == null || 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]; //初始化两条边
for(int j = 0; j < n; j++){
p[0][j] = true;
a[m - 1][j] = true;
} for(int i = 0; i < m; i++){
p[i][0] = true;
a[i][n - 1] = true;
} //判断该点能不能流入两个海
for(int j = 0; j < n; j++){
dfs(matrix,p,0,j);
dfs(matrix,a,m - 1,j);
} for(int i = 0; i < m; i++){
dfs(matrix,p,i,0);
dfs(matrix,a,i,n - 1);
} for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(p[i][j] && a[i][j]){
res.add(new int[]{i,j});
}
}
}
return res;
} public void dfs(int[][] matrix,boolean[][] dp,int row,int col){
dp[row][col] = true;
int m = matrix.length;
int n = matrix[0].length;
for(int i = 0; i < 4; i++){
int p = row + dx[i];
int q = col + dy[i];
if(p < m && p >= 0 && q < n && q >= 0){
if(matrix[row][col] <= matrix[p][q] && dp[p][q] == false) dfs(matrix,dp,p,q);
}
}
}
473. Matchsticks to Square
思路:首先和不是4的整数倍,return false,然后将数组逆序排序,这样能更快的搜索到结果,然后深度优先遍历最后能将数组遍历完并且每条边都能等于target即可

public boolean makesquare(int[] nums) {
if(nums == null || nums.length < 4) return false;
int sum = 0;
for(int num : nums){
sum += num;
}
if(sum % 4 != 0) return false;
//将数组逆序排序以后能更快的搜索到结果
Arrays.sort(nums);
reverse(nums);
return dfs(nums,new int[4],0,sum / 4);
} public boolean dfs(int[] nums,int[] sum,int idx,int target){
if(idx == nums.length && sum[0] == target && sum[1] == target && sum[2] == target){
return true;
} for(int i = 0; i < 4; i++){
if(sum[i] + nums[idx] > target) continue;
sum[i] += nums[idx];
if(dfs(nums,sum,idx + 1,target)) return true;
sum[i] -= nums[idx];
}
return false;
} public void reverse(int[] nums){
int i = 0;
int j = nums.length - 1;
while(i < j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
j--;
}
}
总结
207. Course Schedule:利用拓扑排序,不断取出入度为0的节点,判断最终计数器的值和节点个数是否一致,等价于判断有没有环
133. Clone Graph:利用map存下node.label和node对,方便查找
训练
332. Reconstruct Itinerary:利用map将节点和与之关联的节点保存起来,并利用优先队列进行排序,最后输出结果的时候,优先将堵塞的路径节点添加到结果前面
210. Course Schedule II:思路同207,但是要考虑没有先决条件的情况
200. Number of Islands:深度优先遍历,依次将每个岛中的1都变为0,同时计数
472. Concatenated Words:首先根据字符串长度排序,将短的一依次存入dict用作备选,之后动态规划依次判断后面的长字符串能否用短字符串拼接起来
329. Longest Increasing Path in a Matrix:深度优先遍历,依次找到递增的序列,注意利用之前的结果避免重复搜索
279. Perfect Squares:动态规划,res[n] = Min{ res[n - i * i] + 1 }, n - i * i >= 0 && i >= 1
310. Minimum Height Trees:首先将图的结构用map保存下来,然后取出叶子节点,依次将叶子节点和分支去除,得到新的叶子节点,若最后剩下的节点数<=2则作为结果返回
提示
1、将结果从后往前添加的经验-->332题
2、在二维数组中深度优先遍历的经验 构造两个数组保存四个方向-->417 200 329题
3、拓扑排序的算法-->207 210题

LeetCode---Depth-first && Breadth-first的更多相关文章

  1. (转)The AlphaGo Replication Wiki

    The AlphaGo Replication Wiki 摘自:https://github.com/Rochester-NRT/RocAlphaGo/wiki/01.-Home Contents : ...

  2. Your data vis “Spidey-sense” & the need for a robust “utility belt”

    @theboysmithy did a great piece on coming up with an alternate view for a timeline for an FT piece. ...

  3. Deep Learning中的Large Batch Training相关理论与实践

    背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 在分布式训练时,提高计算通信占比是提高计算加速比的有效手段,当网络通信优化到一 ...

  4. Python Tools for Machine Learning

    Python Tools for Machine Learning Python is one of the best programming languages out there, with an ...

  5. 3.The significance of Books 书本的意义

    3.The significance of Books 书本的意义 (1)A bookless life is an imcomplete life.Books influence the depth ...

  6. AlexNet论文翻译-ImageNet Classification with Deep Convolutional Neural Networks

    ImageNet Classification with Deep Convolutional Neural Networks 深度卷积神经网络的ImageNet分类 Alex Krizhevsky ...

  7. Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来

    Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来 来源 https://www.freebuf.com/articles/paper/184903.ht ...

  8. booklist for machine learning

    Recommended Books Here is a list of books which I have read and feel it is worth recommending to fri ...

  9. Android 性能优化(20)多核cpu入门:SMP Primer for Android

    SMP Primer for Android 1.In this document Theory Memory consistency models Processor consistency CPU ...

  10. 1 - ImageNet Classification with Deep Convolutional Neural Network (阅读翻译)

    ImageNet Classification with Deep Convolutional Neural Network 利用深度卷积神经网络进行ImageNet分类 Abstract We tr ...

随机推荐

  1. AIM Tech Round 3 (Div. 2) B 数学+贪心

    http://codeforces.com/contest/709 题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程. 思路:我们知道,如 ...

  2. vm选项大全

    http://hllvm.group.iteye.com/group/topic/27945 java -XX:后边的总记不住 vm选项大全 http://www.oracle.com/technet ...

  3. 12C 连接方式和 Oracle Easy Connect Naming method

    1.12C 连接方式 PDB is not an instance, so using SID in the connection string will not work. When the dat ...

  4. OpenGL学习-------绘制简单的几何图形

    本次课程所要讲的是绘制简单的几何图形,在实际绘制之前,让我们先熟悉一些概念. 一.点.直线和多边形我们知道数学(具体的说,是几何学)中有点.直线和多边形的概念,但这些概念在计算机中会有所不同.数学上的 ...

  5. iOS7以后的侧滑返回上一页

    我们知道,iOS7以后,导航控制器默认带了侧滑返回功能,但是仅限于屏幕边缘.而且在你自定义leftBarButtonItem等之后侧滑效果就会消失.这种问题怎么解决呢? 首先,我们先来看看系统的这种手 ...

  6. Block 使用场景

    转载自:http://blog.csdn.net/totogo2010/article/details/7839061 代码块本质上是和其他变量类似.不同的是,代码块存储的数据是一个函数体.使用代码块 ...

  7. Struts2--模块包含

    login.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUB ...

  8. google-c-style

    http://zhanxw.com/blog/2011/03/learning-and-applying-coding-style-from-google-in-emacs/ http://stack ...

  9. 微信小程序实例教程(三)

    第七章:微信小程序编辑名片页面开发   编辑名片有两条路径,分为新增名片流程与修改名片流程. 用户手填新增名片流程:   首先跳转到我们的新增名片页面 1 需要传递用户的当前 userId,wx.na ...

  10. IIS7无后缀URL部署问题 MVC4 MVC URL映射 windows server 2008

    前言和中间一段都是我找到问题的过程和思维方法.没兴趣的可以直接跳过看后面的问题和解决. 前言: 问题发生在站点完成后,部署到服务器上.以为这个是最轻松的工作.结果悲剧了.windows server ...