leetcode25word-ladder
题目描述
- 每一次转换只能改变一个单词
- 每一个中间词都必须存在单词字典当中
shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start ="hit"
end ="cog"
dict =["hot","dot","dog","lot","log"]
As one shortest transformation is"hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length5.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
queue<string>Q;
Q.push(start);
int res=1;
while (!Q.empty()){
int qsize=Q.size();
while (qsize--){
string cur_front=Q.front();
Q.pop();
int size=cur_front.size();
for (int i=0;i<size;i++)
{
char ch=cur_front[i];
for (int j=0;j<26;j++){
cur_front[i]='a'+j;
if (cur_front==end) return res+1;
if (dict.find(cur_front)!=dict.end())
{
Q.push(cur_front);
dict.erase(cur_front);
}
}
cur_front[i]=ch;
}
}
res++;
}
return 0;
}
};
leetcode25word-ladder的更多相关文章
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
随机推荐
- matlab中nargin函数输入参数数目
来源:https://ww2.mathworks.cn/help/matlab/ref/nargin.html?searchHighlight=nargin&s_tid=doc_srchtit ...
- DORIS系统概述
DORIS(Doppler Orbitography and Radio-positioning Integrated by Satellite)(多普勒轨道学与无线电定位集成卫星),它是由法国Cne ...
- MeteoInfoLab脚本示例:MODIS Sinusoidal投影HDF数据
MODIS卫星很多陆面数据都是Sinusoidal投影,数据被分为一个个10*10度(赤道地区)的瓦片(http://modis-land.gsfc.nasa.gov/MODLAND_grid.htm ...
- 发布MeteoInfo 1.2.3
提升了对GeoTiff格式数据的读取能力(多个tiles).当然还有MeteoInfoLab功能的提升.下载地址:http://yun.baidu.com/share/link?shareid=669 ...
- ssh登录二次验证,让服务器更安全。
码云地址 sshdTwoVerification 介绍 ssh登录二次验证 问题:现在很多人的Linux服务器可能会被攻击,只校验一次后台用户名密码登录变得不再保险. 当然大家首先要做的是修改ssh服 ...
- 使用python编写正逆序乘法表
# 99乘法表 # 顺序 for i in range(1,10): n = 1 while n <= i: print('{}x{}={}'.format(n,i,n*i),end=' ') ...
- java 环境变量配置(win10)
到官网下载jdk,链接https://www.oracle.com/technetwork/java/javase/downloads/index.html 安装好,进行环境变量配置,打开环境变量 1 ...
- 【C语言编程入门笔记】排序算法之快速排序,一文轻松掌握快排!
排序算法一直是c语言重点,各个算法适应不用的环境,同时,在面试时,排序算法也是经常被问到的.今天我们介绍下快速排序,简称就是快排. 1.快速排序思想: 快排使用 分治法 (Divide and con ...
- oh my zsh 常用插件
date: "2020-10-18T12:36:00+08:00" title: "oh my zsh 常用插件" tags: ["zsh" ...
- 使用composer 显示错误美化
新建comoser.json { "name": "brady_frmwork", "description":"php fram ...