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 ...
随机推荐
- MVC各个层的作用
(1)控制器的作用是调用模型,并调用视图,将模型产生的数据传递给视图.并让相关视图去显示.(2)模型的作用是获取数据并处理数据.(3)视图的作用是将取得的数据进行组织.美化等,并最终向用户终端输出.
- 开发高性能的MongoDB应用—浅谈MongoDB性能优化(转)
出处:http://www.cnblogs.com/mokafamily/p/4102829.html 性能与用户量 “如何能让软件拥有更高的性能?”,我想这是一个大部分开发者都思考过的问题.性能往往 ...
- iOS应用开发之CoreData[转]
我目前的理解,CoreData相当于一个综合的数据库管理库,它支持sqlite,二进制存储文件两种形式的数据存储.而CoreData提供了存储管理,包括查询.插入. 删除.更新.回滚.会话管理.锁管理 ...
- Head First Python之4持久存储
open()用法 # encoding:utf-8 try: # 写模式打开文件,若不存在该文件,则创建 out = open("data.out", "w") ...
- 18、Semantic-UI之进度条
在使用进度条的时候也是必须要结合项目中ajax和后台数据结合使用的. 示例:定义基础进度条 <div class="ui progress"> <div cl ...
- Tomcat配置文件与启动顺序
三个配置应用的位置: 1.conf目录下的server.xml文件:此方式为Eclipse默认配置方法,同时也是三种方式中优先级最高的. <?xml version="1.0" ...
- wp8.1 app退出操作提示
微软的wp8.1 sdk相比之前wp8 sdk以及相关dll类库,微软又重新编译过,相关系统类库也经过精简,删改了部分传统dll库中的方法对象,很多常用方法对象被写进Windows.UI为前缀的命名空 ...
- Backup--压缩备份和TDE
1>对启用TDE的数据库,压缩备份的备份文件大小与未压缩备份的备份文件大小差不多(压缩比为 1 ) 2>对启用TDE的数据库,压缩备份的备份时间远高于未压缩备份 2>对启用TDE的数 ...
- Spring学习(四)——使用Spring JDBC访问数据库
本篇我们将在上一篇http://www.cnblogs.com/wenjingu/p/3824294.html的Demo程序的基础上增加数据持久层和业务层,实现登录验证功能. 1.修改gradle文件 ...
- jsonp的使用记录
最近前端的同事说要写一个手机查看的html5页面,需要我提供数据. 这个很ok啊,立马写了个服务返回数据.但是对方调用不了,因为跨域了. 返回错误如下: Failed to load xxxxxx: ...