[leetcode Summary] BFS

BFS有固定的套路 类似下面这个形式

ArrayList<String> queue = new ArrayList<String>();
queue.add(root);

while(!queue.isEmpty())
{
        String temp = queue.poll();
        for(neigbor : temp)
        {
               queue.add();
        }
}
时间: 2024-08-10 02:00:12

[leetcode Summary] BFS的相关文章

[LeetCode] Combinations (bfs)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 方法:把用queue实现bfs,改为用vector自己实现bfs,没有用额外的内存来存储中间值: class Solution {

[LeetCode] Summary Ranges 总结区间

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Credits:Special thanks to @jianchao.li.fighter for adding this problem and

【LeetCode】BFS(共43题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较对象是 左子树的左儿子和右子树的右儿子, 左子树的右儿子和右子树的左儿子.不要搞错. // 直接中序遍历的话会有错的情况,最蠢的情况是数字标注改一改.. 1 /** 2

LeetCode -- Summary Ranges

题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Credits:Special thanks to @jianchao.li.fighter for adding this probl

LeetCode——Summary Ranges

Description: Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 确定有序数组的区间 需要注意各种边界细节,每次确定区间之后需要清空. public class Solution { pu

[leetcode summary] Dynamic Programming

套路: 一般有两个string的时候 都建二维的数组来存. int[][] dp =new int[len+1][len+1]; 初始情况已设立 dp[0][0] = 1; 递归循环 for() dp[i][j] = dp [i-1][j]; 交答案 dp[len][len]

[leetcode summary] Backtracing

主线是 res helper(res):dfs(): helper(res,num,curr, index, resultOne, visited..) { if(curr==num) 1 res.add(resultOne); 2 res.add(new resultOne); else { for() { if() 1: temp = new resultOne helper(temp...) 2 result.add(); visited; helper(); result.remove(

LeetCode Summary Ranges (统计有序数列范围)

题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. 1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) { 4 if(nums.empty()) return vector<string>(); 5 vector<string> vect; 6 for(int i=0; i<nums.size

[leetCode Summary] LinkedList

几个套路 第一 fast slow 找中点,找位数. 第二 reverse { ListNode last = prev.next; ListNode curr = last.next; while(curr!=null) { last.next = curr.next; curr.next = prev.next; prev.next = curr; curr = last.next; } } 第三搞两个linkedlist然后连起来