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
1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) { 7 * this.val = val; 8 * this.left = this.right = null; 9 * } 10 * } 11 */ 12 13 public class Solution { 14 /** 15 * @param root: A Tree 16 * @return: Level order a list of lists of integer 17 */ 18 public List<List<Integer>> levelOrder(TreeNode root) { 19 // write your code here 20 List<List<Integer>> results = new ArrayList<>(); 21 if(root == null){ 22 return results; 23 } 24 //interface offer(add) poll(pop) 25 Queue<TreeNode> queue = new LinkedList<>(); 26 //put start node into queue 27 queue.offer(root); 28 29 while(!queue.isEmpty()){ 30 //lever x->x+1 31 ArrayList<Integer> currentLevel = new ArrayList(); 32 int size = queue.size(); 33 for(int i =0; i<size;i++){ 34 TreeNode head = queue.poll(); 35 currentLevel.add(head.val); 36 if(head.left != null){ 37 queue.offer(head.left); 38 } 39 if(head.right != null){ 40 queue.offer(head.right); 41 } 42 } 43 results.add(currentLevel); 44 } 45 46 return results; 47 48 49 } 50 }
View Code
queue, offer 和 add的区别
序列化:
xml json thrift(by facebook) protobuf(by google)
序列化算法设计时需要考虑的因素:压缩率 thrift protobuf是为了更快的传输数据和节省存储空间而设计的
可读性:如json
https://www.lintcode.com/problem/graph-valid-tree/description
1 public class Solution { 2 /** 3 * @param n: An integer 4 * @param edges: a list of undirected edges 5 * @return: true if it's a valid tree, or false 6 */ 7 public boolean validTree(int n, int[][] edges) { 8 // write your code here 9 if(n==0){ 10 return false; 11 } 12 13 if(edges.length != n-1){ 14 return false; 15 } 16 //adjacent lists 17 Map<Integer,Set<Integer>> graph = initializeGraph(n,edges); 18 19 Queue<Integer> queue = new LinkedList<>(); 20 Set<Integer> hash = new HashSet<>(); 21 22 queue.offer(0); 23 hash.add(0); 24 25 while(!queue.isEmpty()){ 26 int node = queue.poll(); 27 for(Integer neigh:graph.get(node)){ 28 if(hash.contains(neigh)){ 29 continue; 30 } 31 hash.add(neigh); 32 queue.offer(neigh); 33 } 34 } 35 return hash.size()==n; 36 37 } 38 39 private Map<Integer,Set<Integer>> initializeGraph(int n,int[][]edges){ 40 Map<Integer,Set<Integer>> graph = new HashMap<>(); 41 for(int i=0;i<n;i++){ 42 graph.put(i,new HashSet<>()); 43 } 44 for(int i =0;i<edges.length;i++){ 45 int u = edges[i][0]; 46 int v = edges[i][1]; 47 graph.get(u).add(v); 48 graph.get(v).add(u); 49 } 50 return graph; 51 } 52 }
View Code
转载于:https://www.cnblogs.com/lizzyluvcoding/p/10353640.html
BFS - 20190206相关推荐
- HDU-1459.非常可乐(BFS )
这道题TLE了很多次,原来一直以为将数字化为最简可以让运算更快,但是去了简化之后才发现,真正耗时的就是化简....还和队友学到了用状态少直接数组模拟刚就能过... 本题大意:给出可乐的体积v1,给出两 ...
- HDU1548:A strange lift(Dijkstra或BFS)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:电梯每层有一个数,例如第n层有个数k, 那么这一层只能上k层或下k层,但是不能低于一层或高 ...
- HDU 1429 胜利大逃亡(续) (BFS+位压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- usaco Overfencing 穿越栅栏(BFS)
Overfencing 穿越栅栏 农夫 John 在外面的田野上搭建了一个巨大的用栅栏围成的迷宫.幸运的是,他在迷宫的边界上留出 了两段栅栏作为迷宫的出口.更幸运的是,他所建造的迷宫是一个" ...
- 数据结构--搜索BFS
文章目录 广度优先搜索 典型例题 广度优先搜索 广度优先搜索类似于树的层次遍历过程.它需要借助一个队列来实现.如图2-1-1所示,要想遍历从v0到v6的每一个顶点,我们可以设v0为第一层,v1.v2. ...
- 2017icpc沈阳 G Infinite Fraction Path BFS+剪枝
题意:给一个长度为n的字符串数组,你可以选定起点跳n次,从i点只能跳到(i*i+1)%n的位置,最后求一个最大字典序. 思路:要求最大的,即每一步都是最大,所以将最大的数都入队进行bfs跳下一步. 剪 ...
- HDU - 5876 Sparse Graph 2016 ACM/ICPC 大连网络赛 I题 bfs+set+补图最短路
题目链接 题意:给的补图,让你求一个源点到其他点的最短距离,因为图太稠密了, 用dij以及spfa根本不得行,这里只能用一种我不会方法来进行,这里用了bfs的方法以及set来维护,分别set维护一个未 ...
- 数据结构与算法(7-2)图的遍历(深度优先遍历DFS、广度优先遍历BFS)(分别用邻接矩阵和邻接表实现)
目录 深度优先遍历(DFS)和广度优先遍历(BFS)原理 1.自己的原理图 2.官方原理图 一.邻接矩阵的深度优先遍历(DFS) 1.原理图 2. 过程: 3.总代码 二.邻接表的深度优先遍历(DFS ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边 ...
最新文章
- R可视化多元线性回归模型
- 微软笔试题 2013暑期实习笔试题目
- 插入排序法(思路及代码实现)
- shell和linux的认识
- 8.4 parted:磁盘分区工具
- Java进击C#——应用开发之Asp.net MVC
- Django从理论到实战(part5)--创建我们的第一个应用
- linux终端模拟器app下载,3C终端模拟器app下载-3C终端模拟器v0.9最新版下载 - 91手游网...
- 每日冲刺报告——Day2(Java-Team)
- Python菜鸟入门:day11文件操作
- 【hibernate criteria】hibernate中criteria的完整用法 转
- android开发学习笔记(一)
- 用python编写一个弹球游戏
- 安装ahci驱动sata
- Flask Web——Jinjia2模板的使用
- 常用Linux命令行技巧
- 如何检查SFP光模块的光信号强度?
- 手机卫星定位系统_北斗卫星已覆盖130国上空,那手机上能搜到吗?北斗女神这样比喻...
- 喜讯|云畅科技上榜湖南省上市后备企业名单
- Java 二进制与十六进制字符串相互转换
热门文章
- Hive 内置函数权威指南,操作大全
- Flask 中的Jinja2模板引擎
- Linux mail 命令 不兼容 从 Redhat-release5 迁移到 Redhat-release6以上
- 王道 —— 操作系统的运行机制和体系结构
- leetcode —— 41. 缺失的第一个正数
- 机器学习实战(6):SVM-SMO-核函数 手写识别
- spfa(STL写法)简单讲解+最短路(spfa)例题 acm寒假集训日记22/1/7
- java怎么判断按钮是否被点击_怎么判断肌肉训练后是否已经恢复
- java程序样例_一个完整的java程序示例
- java复制sheet_Java对excel中的sheet进行拷贝