leetcode BFS
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的更多相关文章
- [LeetCode] BFS解决的题目
一.130 Surrounded Regions(https://leetcode.com/problems/surrounded-regions/description/) 题目: 解法: 这道题 ...
- leetcode BFS解题思路
Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- (转)CentOS7安装KVM虚拟机详解
原文:https://github.com/jaywcjlove/handbook/blob/master/CentOS/CentOS7%E5%AE%89%E8%A3%85KVM%E8%99%9A%E ...
- hadoop-2.6.0-cdh5.4.5.tar.gz(CDH)的3节点集群搭建(含zookeeper集群安装)
前言 本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接 http://www.cnblogs.com/zlslch/p/ ...
- MyBatis Generator 详解(转)
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- oracle:ORA-00911: 无效字符 问题和解决---Eclipse中的SQL语句不能加分号
eclipse中原sql: 异常: 原因:Eclipse中的SQL语句不能加分号 去掉分号,正常执行,插入成功. 这里把id设为了主键,具有唯一性,重复插入同一id执行插入失败,ORA-00001号错 ...
- OpenStack Object Storage(Swift)概述
概述 OpenStack Object Storage(Swift)是OpenStack开源云计算项目的子项目之一,被称为对象存储,提供了强大的扩展性.冗余和持久性. Swift并不是文件系统或者实时 ...
- 第5章 scrapy爬取知名问答网站
第五章感觉是第四章的练习项目,无非就是多了一个模拟登录. 不分小节记录了,直接上知识点,可能比较乱. 1.常见的httpcode: 2.怎么找post参数? 先找到登录的页面,打开firebug,输入 ...
- get/post 接口调用
content-type: application/~~~~~ /// <summary> /// Post数据到网站 /// </summary> /// <para ...
- 激活 IntelliJ IDEA
1.点击下面的链接下载 JetbrainsIdesCrack-4.2-release.jar 链接:https://pan.baidu.com/s/1eNY_bwxF7Efl4QG0yh6l1A 提 ...
- C# 工具类之数据库链接
一.SQL Server 相关 /// <summary> /// 数据库的通用访问代码 /// 此类为抽象类, /// 不允许实例化,在应用时直接调用即可 /// </summa ...
- spring AOP为什么配置了没有效果?
spring Aop的配置一定要配置在springmvc配置文件中 springMVC.xml 1 <!-- AOP 注解方式 :定义Aspect --> <!-- ...