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 ...
随机推荐
- php学习记录,使用script脚本
echo "<script>alert()</script>"; 原来还能这么用,之前以为echo就是普通的用来打印 同时还可以在script标签下使用lo ...
- eclipse中的一些快捷键
1.内容提示 Alt+/ 2.快速修复 ctrl+/ 3.导包 ctrl+shift+o 4.格式代码块 ctrl+shift+o 5.向前向后 Alt+方向键 6.添加注释 ctrl+shift+/ ...
- React框架的基本使用和了解
React: React详解: 安装react 脚手架工具: npm install -g create-react-app create-react-app 项目名称 cnpm react-dom ...
- Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网关Ocelot+Consul
相关文章 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网 ...
- PHP中的日期相关函数(三)
之前我们已经介绍过了 PHP 的一些相关的日期操作对象,今天我们就来学习剩下的那些面向过程的使用方式.当然,如果是和 DateTime 类中相似的方法我们就不再进行介绍了.另外,Date() 和 ti ...
- ci框架 自定义配置方法
系统自动在Application文件夹下生成的config.php文件,采用key-value关联数组的形式来存放配置项和值.为了使结构更清晰,手动新建另外一个配置文件myconfig.php,所采用 ...
- 版本管理 - Git 使用入门
Git 是一个分布式的版本管理系统,而 SVN 是一个集中式管理系统. 版本控制 Git与SVN的对比 对比 SVN Git 工作方式 集中式 分布式 文件管理 增量式 系统快照 权限控制 对团队中参 ...
- 定要过python二级 真题 第四套
第一模块 基本操作 1.print(" { } " . format(s)) 记住 " { }" ...
- 华为云计算IE面试笔记-eBackup有哪几种备份组网方式,各备份组网方式主要的应用场景及备份流程?
应用场景: LAN-Base一般用于备份数据量小,且对备份窗口没有特殊要求的场景,此类场景下备份服务器和备份代理一般是虚拟机部署. LAN-Free一般用于备份数据量较大,且对备份窗口要求比较严格的场 ...
- 5分钟实现用docker搭建Redis集群模式和哨兵模式
如果让你为开发.测试环境分别搭一套哨兵和集群模式的redis,你最快需要多久,或许你需要一天?2小时?事实是可以更短. 是的,你已经猜到了,用docker部署,真的只需要十几分钟. 一.准备工作 拉取 ...