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 ...
随机推荐
- PSR2规范
为了尽可能的提升阅读其他人代码时的效率,下面例举了一系列的通用规则,特别是有关于PHP代码风格的.各个成员项目间的共性组成了这组代码规范.当开发者们在多个项目中合作时,本指南将会成为所有这些项目中共用 ...
- javascript中prototype与__proto__
1.prototype:构造函数独有的属性: __proto__:每个对象都有一个名为__proto__的属性: 注意:每个构造函数(自带与自创)都有一个prototype的属性,构造函数的proto ...
- Hadoop Metrics2
来源:Hadoop Metrics2 Metrics are collections of information about Hadoop daemons, events and measureme ...
- 13 Timer和TimerTask 示例
定时器是一个应用十分广泛的线程工具,可用于调度多个定时任务以后台线程的方式执行.在Java中,可以通过Timer和TimerTask类来实现定义调度的功能 1 Timerjava.lang.Objec ...
- Numpy API
Numpy API 矩阵操作 np.squeeze(mat): 将mat降维 np.linalg.norm(x, axis=1, keepdims=True): keepdim=True是防止出现sh ...
- (完整)爬取数据存储之TXT、JSON、CSV存储
一.文件存储 1. TXT文本存储 例:知乎发现页面,获得数据存成TXT文本 import requests from pyquery import PyQuery as pq url="h ...
- [转]RDL(C) Report Design Step by Step 3: Mail Label
本文转自:http://www.cnblogs.com/waxdoll/archive/2006/09/02/493350.html Crystal Report在报表向导中提供了三种向导类型给用户进 ...
- 第4章 scrapy爬取知名技术文章网站(1)
4-1 scrapy安装以及目录结构介绍 安装scrapy可以看我另外一篇博文:Scrapy的安装--------Windows.linux.mac等操作平台,现在是在虚拟环境中安装可能有不同. 1. ...
- css3 响应式布局 Media Query
1.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简单说就是一个网站能够兼容多个终端. 2.响应式布局的优缺点? 优点:面对不同分辨率设备灵活性强,快捷 ...
- 判断浏览器 插件 jquery.ua.js
判断浏览器 插件 jquery.ua.js /*! * jquery.ua.js * @link https://github.com/cloudcome/jquery.ua * @author yd ...