leetcode BFS

1. word ladder

 1 class Solution
 2 {
 3 public:
 4     int ladderLength(string beginWord, string endWord, unordered_set<string> &wordDict)
 5     {
 6         queue<string> q;
 7         q.push(beginWord);
 8         unordered_map<string, int> umap;
 9         umap[beginWord] = 1;
10         for (q.push(beginWord); !q.empty(); q.pop())
11         {//这里用for的好处是这本身是一个模块化的过程:先入队;当队非空时进行循环,同时每个循环结束时都要把当前元素出队。用for不容易遗漏q.pop()。
12             string word = q.front();
13             int step = umap[word] + 1;
14             for (int i = 0; i<word.size(); i++)
15             {
16                 for (char c = ‘a‘; c <= ‘z‘; c++)
17                 {
18                     if (word[i] != c)
19                     {
20                         char tmp = word[i];
21                         word[i] = c;
22                         if (word == endWord)//this line should here, not in the if statement below,coz endWord may not be in dict
23                             return step;
24                         if (wordDict.find(word) != wordDict.end() && umap.find(word) == umap.end())
25                         {
26                             umap[word] = step;
27                             q.push(word);
28                         }
29                         word[i] = tmp;//don‘t forget to restore
30                     }
31                 }
32             }
33         }
34         return 0;
35     }
36 };
时间: 2025-01-05 16:36:20

leetcode BFS的相关文章

[LeetCode] BFS解决的题目

一.130  Surrounded Regions(https://leetcode.com/problems/surrounded-regions/description/) 题目: 解法: 这道题的意思是将所有被X包围的O都变为X(边缘的不算),我们可以维护一个队列,先把四周的O的位置放进队列中,并把这个位置的值变为Y.然后每次从队列中拿出一个位置,把这个位置四周为O的位置的值变为Y,再把这个位置放进队列(为什么要先变值再进队列呢?在下一道题中会说).一直到队列为空时,我们就成功地把所有直接

Leetcode bfs&amp;dfs Binary Tree Postorder Traversal II

Binary Tree Level Order Traversal II Total Accepted: 16983 Total Submissions: 54229My Submissions Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For ex

Leetcode bfs&amp;dfs Binary Tree Postorder Traversal

Binary Tree Level Order Traversal Total Accepted: 20571 Total Submissions: 66679My Submissions Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,2

LeetCode[BFS]: Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[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] Surrounded Regions(DFS、BFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

[LeetCode]513 Find Bottom Left Tree Value(BFS)

题目链接:https://leetcode.com/problems/find-bottom-left-tree-value/?tab=Description 题意:找到二叉树里最底下的最靠左的叶子节点的值. 看到input的时候吓哭了,以为是要处理这种输入.看到代码填空部分才缓过来- 直接bfs,遇到更深的就更新,每次都让最左的先入队列.就能保证每次更新到的答案都是最左的. 1 /** 2 * Definition for a binary tree node. 3 * struct Tree