首页 > 其他分享 >127. Word Ladder (很重要!!!)

127. Word Ladder (很重要!!!)

时间:2022-12-01 20:05:26浏览次数:60  
标签:tmp ch Word int Ladder front beginWord 127 endWord


beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWordto endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:

beginWord = ​​"hit"​

endWord = ​​"cog"​

wordList = ​​["hot","dot","dog","lot","log"]​​​​"hit" -> "hot" -> "dot" -> "dog" -> "cog"​​,

return its length ​​5​​.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

​Subscribe​​ to see which companies asked this question

class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
queue<string> Q;
set<string>visited;
Q.push(beginWord);
visited.insert(beginWord);
int cnt = 1;
while (!Q.empty()){
cnt++;
int size = Q.size();
while (size--){
string front = Q.front();
Q.pop();
for (int i = 0; i < front.size(); i++){
for (char ch = 'a'; ch <= 'z'; ch++){
if (ch == front[i]) continue;
string tmp = front;
tmp[i] = ch;
if (wordList.count(tmp) && !visited.count(tmp)){
if (tmp == endWord) return cnt;
Q.push(tmp);
visited.insert(tmp);
}
}
}
}
}
return 0;
}
};




标签:tmp,ch,Word,int,Ladder,front,beginWord,127,endWord
From: https://blog.51cto.com/u_15899184/5904046

相关文章

  • 79. Word Search
    Givena2Dboardandaword,findifthewordexistsinthegrid.Thewordcanbeconstructedfromlettersofsequentiallyadjacentcell,where"adjacent"cell......
  • WordPress编辑器支持Word文档粘贴
    ​ ueditor粘贴不能粘贴word中的图片是一个很头疼的问题,在我们的业务场景中客户要求必须使用ueditor并且支持word的图片粘贴,因为这个需求头疼了半个月,因为前端方面因为安......
  • WordPress编辑器支持Word文档上传
    ​ 当前功能基于PHP,其它语言流程大致相同 1.新增上传wordjson配置在ueditor\php\config.json中新增如下配置:     /* 上传word配置 */    "wordAction......
  • error: rpmdb: BDB0113 Thread/process 14536/140712790841152 failed: BDB1507 Threa
    yumremovedockererror:rpmdb:BDB0113Thread/process14536/140712790841152failed:BDB1507ThreaddiedinBerkeleyDBlibraryerror:db5error(-30973)fromdb......
  • WordPress编辑器支持Word文档导入
    ​ 当前功能基于PHP,其它语言流程大抵相同。大概流程:1.将docx文件上传到服务器中2.使用PHPoffice/PHPword实现将word转换为HTML3.将HTML代码返回并赋值到编辑器中......
  • H5 下载word文件时预览失败,提示无法预览此文件,它可能已损坏,有部分可可以查看预览
    移动端H5下载文件功能时,发现部分word文件可以在线打开有些无法打开,提示“无法预览此文件,它可能已损坏”;但是在Pc端下载是可以正常打开的,移动端是不行。代码如下:byte[]arr......
  • 隧道调试本地IIS Express报400错误,127.0.0.1无法访问
    我们在使用​​https://natapp.cn​​​时,难免会本地进行调试代码,自己要申请一个隧道域名,比如:​​http://ls.nat600.top​​,然后在访问的时候加上端口会报错,使用localhost加......
  • WordPress编辑器支持Word文档一键粘贴
    ​ 百度ueditor新增的将word内容导入到富文本编辑框的功能怎么没有啊,...ueditor实现word文档的导入和下载功能的方法:1、UEditor没有提供word的导入功能,只能说是粘贴复......
  • 如何通过Java代码给Word文档添加水印?
    Word中可以为文档添加的水印分为两种形式:文字水印和图片水印。水印是一种数字保护的手段,在文档上添加水印可以传达有用信息,或者在不影响正文文字显示效果的同时,为打印文档增......
  • WordPress编辑器支持Word文档一键导入
    ​图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,......