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 ...
随机推荐
- spring boot快速入门 9: 单元测试
进行单元测试: service第一种方式: 第一步:在指定service中创建一个方法进行测试 /** * 通过ID查询一个女生的信息 * @param id * @return */ public ...
- 【Ubuntu】执行定时任务(cron)
1.打开定时任务配置文件 crontab -e 2.编写定时任务时间 命令和脚本例如: /3 * * * * /soft/config/test.sh 前5个字段为时间,后面的一个为命令 前5个含义为 ...
- 033-JsonUtils 工具类模板
模板一:使用的是jackson package cn.e3mall.common.utils; import java.util.List; import com.fasterxml.jackson. ...
- ZOJ 2971 Give Me the Number
Give Me the Number Numbers in English are written down in the following way (only numbers less than ...
- 使用go实现的lisp
去年10月份的时候,就有这个打算了. 也是在上个月左右,抽空弄出来了个go语言实现的lisp. 当然,不能和common lisp比,函数的数量是远远不如的,也不能自己定义类型/类,同时宏系统也非常简 ...
- CentOS7手动修改系统时间
CentOS7 永久修改系统时间 安装在虚拟机上的CentOS7的时间分为系统时间和硬件时间.二者都修改,重启系统(init 6 )才会永久生效.修改步骤如下 查看当前系统时间 date 修改当 ...
- jQuery全屏滚动插件fullPage.js中文帮助文档API
jQuery全屏滚动插件fullPage.js中文帮助文档API 发现了一个fullPage.js插件,于是百度了一下,还就是这个插件的作用,其实有很多网站都做了全屏滚动的特效,效果也很好看,今天 ...
- WPF 小知识点001
1.DataGrid 单选事件 <DataGrid MinHeight="150" AutoGenerateColumns="Fa ...
- 微信WeUI扩展组件
主要包括 下拉刷新pullToRefresh downRefresh.html 主要的代码是$(document.body).pullToRefresh(); <div class=" ...
- MySQL (一)(未完成)
并发控制 读写锁 读锁: 共享锁 写锁: 排它锁 颗粒度 表锁,MySQL中开销最小的锁 行锁,MySQL中开销最大的锁 事务 ACID特性 原子性(Automatic) 隔离性(Isolation) ...