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 ...
随机推荐
- 编程体系结构(06):Java面向对象
本文源码:GitHub·点这里 || GitEE·点这里 一.基础概念 1.面向对象概念 面向对象编程的主要思想是把构成问题的各个事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙一 ...
- JS-YAML -YAML 1.2 JavaScript解析器/编写器
下载 JS-YAML -YAML 1.2 JavaScript解析器/编写器JS-YAML -YAML 1.2 JavaScript解析器/编写器 在线演示 这是YAML的实现,YAML是一种对人友好 ...
- RHSA-2017:3075-重要: wget 安全更新(代码执行)
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...
- Vue.js 学习笔记之四:Vue 组件基础
到目前为止,这个系列的笔记所展示的都是一些极为简单的单页面 Web 应用程序,并且页面上通常只有几个简单的交互元素.但在实际生产环境中,Web 应用程序的用户界面往往是由多个复杂的页面共同组成的.这时 ...
- 多测师讲解selenium_iframe框定位_高级讲师肖sir
iframe 框定位方法: 查看iframe框 京东点击登录定位元素 定位qq: qq登录定位的元素 查找iframe框 定位iframe框 from selenium import webdrive ...
- IDEA 简拼输入
1. sout = System.out.println(); 2. soutp = System.out.println(""); 3. soutv = System.out.p ...
- JDBC的学习(一)
JDBC的学习(一) 概念 所谓英文简写的意思是:Java DataBase Connectivity ,即 Java数据库的连接,用Java语言来操作数据库 本质 简单的来说,就是写这个JDBC的公 ...
- pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)
前言 使用命令行执行pytest用例的时候,会在 terminal 终端打印整个用例的测试结果: .代表通过的用例 F代表失败的用例 E代表异常的用例 如果我们不喜欢这种报告结果,可以通过 pytes ...
- 【Luogu】P3369 【模板】普通平衡树(树状数组)
P3369 [模板]普通平衡树(树状数组) 一.树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构. ...
- matplotlib直方图
import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib.font_manager import FontPro ...