1. word ladder

 class Solution
{
public:
int ladderLength(string beginWord, string endWord, unordered_set<string> &wordDict)
{
queue<string> q;
q.push(beginWord);
unordered_map<string, int> umap;
umap[beginWord] = ;
for (q.push(beginWord); !q.empty(); q.pop())
{//这里用for的好处是这本身是一个模块化的过程:先入队;当队非空时进行循环,同时每个循环结束时都要把当前元素出队。用for不容易遗漏q.pop()。
string word = q.front();
int step = umap[word] + ;
for (int i = ; i<word.size(); i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
if (word[i] != c)
{
char tmp = word[i];
word[i] = c;
if (word == endWord)//this line should here, not in the if statement below,coz endWord may not be in dict
return step;
if (wordDict.find(word) != wordDict.end() && umap.find(word) == umap.end())
{
umap[word] = step;
q.push(word);
}
word[i] = tmp;//don't forget to restore
}
}
}
}
return ;
}
};

leetcode BFS的更多相关文章

  1. [LeetCode] BFS解决的题目

    一.130  Surrounded Regions(https://leetcode.com/problems/surrounded-regions/description/) 题目: 解法: 这道题 ...

  2. leetcode BFS解题思路

    Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  5. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  6. [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  7. [LeetCode] 787. Cheapest Flights Within K Stops_Medium tag: Dynamic Programming, BFS, Heap

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  8. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  9. [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

随机推荐

  1. 【Ubuntu】执行定时任务(cron)

    1.打开定时任务配置文件 crontab -e 2.编写定时任务时间 命令和脚本例如: /3 * * * * /soft/config/test.sh 前5个字段为时间,后面的一个为命令 前5个含义为 ...

  2. lambda modern C++

    Lambda expressions (since C++11) Syntax   [ captures ] <tparams>(optional)(c++20) ( params ) s ...

  3. Windows Git Bash命令行下创建git仓库并更新到github

    大二的时候就听过老师说有一个叫git的版本管理工具,当时只是听老师说说而已,也没有去使用它,因为当时用过svn,就感觉自己没多少东西需要git管理. 最近几天,我经常在开源中国看别人的帖子,看到别人对 ...

  4. 【文档】一、Mysql Binlog概述

    Binlog是一系列日志文件,他们包含的内容是Mysql数据内容的改变.如果想开启binlog功能,需要在启动时带上--log-bin参数. binlog是从Mysql3.23.14版本开始的.它包含 ...

  5. 四大组件之Activity——生命周期

    1.完整的Activity生命周期 包括onCreate() -> onStart() -> onResume() -> onPause() -> onStop -> o ...

  6. JavaScript数据结构-18.图结构广度优先和最短路径

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 2.2.2 加入factory机制

    上一节给出了一个只有driver.使用UVM搭建的验证平台.严格来说这根本就不算是UVM验证平台,因为UVM的特性几乎一点都没有用到.像上节中my_driver的实例化及drv.main_phase的 ...

  8. Hibernate 4.3 SessionFactory

    Configuration configuration = new Configuration().configure(); //以下这两句就是4.3的新用法 StandardServiceRegis ...

  9. yii 页面加载完成后弹出模态框

    <?php $js = <<<JS $('#page-modal').modal('show');//页面加载完显示模态框 $('.modal-dialog').css('wi ...

  10. DButils分析

    package com.ldf.utils; import java.sql.Connection; public class DBUtils { private static String driv ...