word-ladder leetcoder C++
Given two words (start and end), and a dictionary, find the length of 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. All words contain only lowercase alphabetic characters.
C++
class Solution {
bool diff(string a,string b){
int c=0;
for(int i=0;i<a.size();i++)
if(a[i]!=b[i]) c++;
if(c==1) return true;
return false;
}
public:
int ladderLength(string start,string end,unordered_set<string> &dict){
dict.insert(end);
dict.erase(start);
queue<string> q;
q.push(start);
int length=0;
while(q.size()>0){
length++;
int QueueLength=q.size();
for(int i=0;i<QueueLength;i++){
start=q.front();
q.pop();
if(start==end) return length;
for(unordered_set<string >::iterator iter=dict.begin();iter!= dict.end();){
if(diff(start,*iter)){
q.push(*iter);
dict.erase(iter++);
}else iter++;
}
}
}
return 0;
}
int ladderLength2(string start, string ends, unordered_set<string> &dict) {
int res=1;
queue<string> q;
q.push(start);
while(!q.empty()){
int size=q.size();
while(size>0){
string top=q.front();
q.pop();
size--;
if(diff(top,ends)) return res+1;
for(unordered_set<string >::iterator i =dict.begin();i!=dict.end();){
if(diff(*i,top)){
q.push(*i);
dict.erase(i++);
}else i++;
}
}
res++;
}
return 0;
}
};
word-ladder leetcoder C++的更多相关文章
- [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 ...
随机推荐
- String底层使用是char数组还是byte数组
结论:jdk1.8及以前String底层使用是char[],1.9开始使用byte[] jdk1.8 jdk13
- 离散化模板题 I ——重复元素离散化后的数字相同
离散化模板题 I --重复元素离散化后的数字相同 题目描述 现有数列A1, A2, ⋯, An,数列中可能有重复元素. 现在要求输出该数列的离散化数列,重复元素离散化后的数字相同. 输入 第一行,一 ...
- 手机UI自动化之显示点触位置(触摸轨迹)
上期回顾:Airtest源码分析--Android屏幕截图方式 不管是用Appium还是Airtest,或是其他手机UI自动化工具,你是不是经常遇到这种情况,代码明明执行了click或swipe,怎么 ...
- python字典时间日期
字典时间日期 学习完本篇,你将会深入掌握 字典操作. 1.如何新建一个字典 2.如何正序以及反序访问字典 3.如何更新字典元素 4.如何删除字典 日期时间操作 1.如何打印一个2021-9-20 17 ...
- 学习PHP中的任意精度扩展函数
今天来学习的是关于数学方面的第一个扩展.对于数学操作来说,无非就是那些各种各样的数学运算,当然,整个程序软件的开发过程中,数学运算也是最基础最根本的东西之一.不管你是学得什么专业,到最后基本上都会要学 ...
- PHP获取当前网址路径
$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] // 获取来源 function ...
- CentOS7安装Docker遇到的问题笔记
笔记/朱季谦 以下是笔者本人学习搭建docker过程当中记录的一些实践笔记,过程当中也遇到了一些坑,但都解决了,就此记录,留作以后再次搭建时可以直接参考. 一.首先,先检查CentOS版本,保证在Ce ...
- phpQuery
以下资料均来自官方文档,官方文档地址:https://code.google.com/archive/p/phpquery/wikis 基础 示例 phpQuery::newDocumentFileX ...
- 网站优化之mysql优化
一,网站优化之mysql优化:1.前缀索引,可以通过前缀去识别唯一性,把这个前缀作为索引内容,可以节省存储索引的空间,从而提高索引的查询速度.distinct 排重操作2,2.in条件索引使用同时查询 ...
- python FastAPI 初接触
先吹一波: 原来写接口可以这么简单!!! 简单到没朋友 . 中文官网:https://fastapi.tiangolo.com/zh/tutorial/header-params/ 且天然支持异步处理 ...