BFS - 20190206
1.二叉树 BFS
2.拓扑排序 重点 BFS
3.棋盘上的宽搜 BFS
图的遍历
层级遍历,由点及面,拓扑排序,简单图的最短路径
如果题目问最短路径:可能是BFS或者DP, 最长路径:DFS
queue 的数组实现
1.二叉树的BFS
https://www.lintcode.com/problem/binary-tree-level-order-traversal/description
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
// write your code here
List<List<Integer>> results = new ArrayList<>();
if(root == null){
return results;
}
//interface offer(add) poll(pop)
Queue<TreeNode> queue = new LinkedList<>();
//put start node into queue
queue.offer(root); while(!queue.isEmpty()){
//lever x->x+1
ArrayList<Integer> currentLevel = new ArrayList();
int size = queue.size();
for(int i =0; i<size;i++){
TreeNode head = queue.poll();
currentLevel.add(head.val);
if(head.left != null){
queue.offer(head.left);
}
if(head.right != null){
queue.offer(head.right);
}
}
results.add(currentLevel);
} return results; }
}
queue, offer 和 add的区别
序列化:
xml json thrift(by facebook) protobuf(by google)
序列化算法设计时需要考虑的因素:压缩率 thrift protobuf是为了更快的传输数据和节省存储空间而设计的
可读性:如json
https://www.lintcode.com/problem/graph-valid-tree/description
public class Solution {
/**
* @param n: An integer
* @param edges: a list of undirected edges
* @return: true if it's a valid tree, or false
*/
public boolean validTree(int n, int[][] edges) {
// write your code here
if(n==0){
return false;
}
if(edges.length != n-1){
return false;
}
//adjacent lists
Map<Integer,Set<Integer>> graph = initializeGraph(n,edges);
Queue<Integer> queue = new LinkedList<>();
Set<Integer> hash = new HashSet<>();
queue.offer(0);
hash.add(0);
while(!queue.isEmpty()){
int node = queue.poll();
for(Integer neigh:graph.get(node)){
if(hash.contains(neigh)){
continue;
}
hash.add(neigh);
queue.offer(neigh);
}
}
return hash.size()==n;
}
private Map<Integer,Set<Integer>> initializeGraph(int n,int[][]edges){
Map<Integer,Set<Integer>> graph = new HashMap<>();
for(int i=0;i<n;i++){
graph.put(i,new HashSet<>());
}
for(int i =0;i<edges.length;i++){
int u = edges[i][0];
int v = edges[i][1];
graph.get(u).add(v);
graph.get(v).add(u);
}
return graph;
}
}
BFS - 20190206的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
随机推荐
- Ubuntu的SWAP设置
1. 在Ubuntu中配置使用新创建的Swap分区 Command list: 查找Swap分区的UUID sudo blkid 在/ect/fstab中加入新的Swap分区 sudo gedit / ...
- echo $[1 + 2] shell中 $[] 在bash中同$(()),用于算术计算
shell脚本编写:echo $[ 11#8+1] 输出结果是几,为什么,怎么算来的? 摘自:https://zhidao.baidu.com/question/334766451.html 结 ...
- Web测试实践--Rec 4
累计完成任务情况: 阶段内容 参与人 整理小组工作记录,维护小组博客 小尹 分析产品并撰写文档相关板块 小靳 撰写文档中对被测系统进行功能性分析相关板块 小龙.小黄 撰写文档中用户调研相关板块 小熊 ...
- Web测试实践-任务进度-Day02
小组成员 华同学.郭同学.覃同学.刘同学.穆同学.沈同学 任务进度 在经过任务分配阶段后,大家都投入到了各自的任务中,以下是大家今天任务的进度情况汇总. 华同学 & 刘同学(任务1) 1.对爱 ...
- Selenium模拟浏览器初识
Seleniumd介绍 在写Python爬虫的时候,最麻烦的不是那些海量的静态网站,而是那些通过JavaScript获取数据的站点.Python本身对js的支持不好,所以就有良心的开发者来做贡献了,这 ...
- mysql基础操作记录
安装mysql Mac使用homebrew安装mysql,命令行执行以下命令:brew install mysql 启动mysql服务 安装完成后执行start 命令. ➜ ~ mysql.serve ...
- ubuntu 16.04安装ceph集群(双节点)
Ceph是一个分布式存储,可以提供对象存储.块存储和文件存储,其中对象存储和块存储可以很好地和各大云平台集成.一个Ceph集群中有Monitor节点.MDS节点(可选,用于文件存储).至少两个OSD守 ...
- Alpha冲刺(四)
Information: 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Details: 组员1(组长)柯奇豪 过去两天完成了哪些任务 文章基本的存储.列表生成显示 展示G ...
- 解决NIOS II工程移动在磁盘上位置后project无法编译问题
说明:本文档于2017年3月4日由小梅哥更新部分内容,主要是增加了讲解以Quartus II13.0为代表的经典版本和以15.1为代表的更新版本之间,解决问题的一些小的差异. 如果用户只是想快速解决问 ...
- Rsync+Inotify实现文件自动同步
1>rsync概述 rsync的优点与不足 rsync与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求 ...