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 ...
随机推荐
- 【数量技术宅|金融数据分析系列分享】为什么中证500(IC)是最适合长期做多的指数
更多精彩内容,欢迎关注公众号:数量技术宅.探讨数据分析.量化投资问题,请加技术宅微信:sljsz01 投资股票指数相比个股的优势 我们在投资股票的时候,如果持仓集中在一只或者有限几只股票上,恰好不幸遇 ...
- ORA-00019: maximum number of session licenses exceeded 超出最大会话许可数
ORA-00019: maximum number of session licenses exceededORA-00019: 超出最大会话许可数 Cause: All licenses ...
- c# 误区系列(一)
前言 整理很早以前认为的一些误区,准备整理一个系列.新手可以看下,然后大佬指点一下是否哪些地方错了. 正文 值类型存在栈上,引用类型存在堆上 很多人认为用这句话来解释值类型和栈类型的区别,甚至有些文章 ...
- java字符流与字节流的区别是什么
java中字符流与字节流的区别: 1.字节流操作的基本单元为字节:字符流操作的基本单元为Unicode码元. 2.字节流默认不使用缓冲区:字符流使用缓冲区. 3.字节流通常用于处理二进制数据,实际上它 ...
- 从Linux源码看Socket(TCP)的bind
从Linux源码看Socket(TCP)的bind 前言 笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码,是一件Exciting的事情. 今天笔者就来从Linux源码的角度看下Server ...
- BUUCTF-数据包中的线索 1
打开题目里面一个pcapng的文件,于是我们拖进wireshark流量分析一波 发现有http的流量,于是我们过滤http 让后追踪http,然后发现base64的编码, 于是我们看到了base64编 ...
- 网络最大流Dinic
1.什么是网络最大流 形象的来说,网络最大流其实就是这样一个生活化的问题:现在有一个由许多水管组成的水流系统,每一根管道都有自己的最大通过水流限制(流量),超过这个限制水管会爆(你麻麻就会来找你喝茶q ...
- centos8平台使用stress做压力测试
一,安装stress: 说明:el8的源里面还没有,先用el7的rpm包 [root@centos8 source]# wget https://download-ib01.fedoraproject ...
- jquery ui,拖拽,dragsort
一.导入js.导入jquery.dragsort.js外还需要导入jQuery. 二.HTML部分 <!DOCTYPE html> <html> <head> &l ...
- Django折腾日记(django2.0)
新建项目 django-admin startproject mysite 运行 python manage.py runserver 创建一个应用 python manage.py startapp ...