Word Ladder 未完成
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, 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 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.
每次只能改变一个字符,求最短路径。
开始想法为列出二维矩阵,找出变化一次,变化两次,知道变化为end,从而求最短路径。然而发现需要内存过多,同时超时。
改为采用BFS,这样首先找到的肯定是最短路径。但是同样超时。看到网上都是用java实现的,不知道是什么问题。
class Solution {
private:
int isOneDiff(string beginWord, string endWord)
{
int n=beginWord.size();
int m=endWord.size();
if(n!=m) return -;
int count=;
for(int i=;i<n;i++)
{
if(beginWord[i]!=endWord[i])
count++;
}
if(count>) return -;
return count;
}
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
int n=wordDict.size();
if(beginWord.empty()||endWord.empty()||n<||beginWord.size()!=endWord.size()||isOneDiff(beginWord,endWord)==)
return ;
if(isOneDiff(beginWord,endWord)==)
return ;
if((wordDict.find(beginWord)!=wordDict.end())&&(wordDict.find(endWord)!=wordDict.end())&&(n==))
return ;
queue<string> q;
map<string,int> wordmap;
int wordlength=beginWord.size();
int count=;
q.push(beginWord);
wordmap.insert(pair<string,int>(beginWord,count));
while(!q.empty())
{
string tmpword=q.front();
count=wordmap[tmpword];
q.pop();
for(int i=;i<wordlength;i++)
{
for(char j='a';j<='z';j++)
{
if(j==tmpword[i]) continue;
tmpword[i]=j;
if(tmpword==endWord) return count+;
if(wordDict.find(tmpword)!=wordDict.end())
{
q.push(tmpword);
wordmap.insert(pair<string,int>(tmpword,count+));
}
}
}
}
return ;
}
};
Word 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 ...
随机推荐
- iOS开发笔记1:ToDoList、相册、地图应用及新浪微博
前段时间做了一些小东西,一些关键点记录总结如下 1.ToDoList 主要是使用UITableView以及NSUserDefaults完成任务管理,可新建.编辑及删除任务. 因为是Demo性质,所以利 ...
- 关于UI资源获取资源的好的网站
前言:和我一样喜欢UI的一定喜欢这里的内容. 下面是关于sketch资源获取网页,点击图片就能进入: 连接是:https://github.com/JakeLin 居然意外百度到Sketch中国,还提 ...
- IOS之Foundation之探究学习Swift实用基础整理<一>
import Foundation //加载网络数据,查找数据的字符串 let dataurl = "http://api.k780.com:88/?app=weather.city& ...
- [转]android访问网络:java.net.ConnectException: localhost/127.0.0.1:8888 - Connection refused
这对刚学会向tomcat模拟的本地服务器发送请求的同学非常重要! 转自:http://wing123.iteye.com/blog/1873763 描述:在做注册功能的时候,向本地服务器:127.0. ...
- 关于配置并发访问的服务器apache、nginx
一. apache,nginx比较 关于Apache与Nginx的优势比较 (apache计算密集型 nginx io密集型 各有优势,不存在谁取代谁) 二.nginx 基于nginx ...
- 附带详细注释的log4net的app.config文件配置例子
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- IO流02--毕向东JAVA基础教程视频学习笔记
提要 08 自定义装饰类09 LineNumberReader10 MyLineNumberReader11 字节流File读写操作12 拷贝图片13 字节流的缓冲区14 自定义字节流的缓冲区-rea ...
- 设计模式C#实现(九)——工厂方法模式和简单工厂
工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. 构成: 1.Product工厂方法创建的对象的接口 2.Concrete ...
- 烂泥:kvm安装windows系统蓝屏
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近一直在学习有关KVM的知识,实验一直是在虚拟机VM中进行的.今天刚好公司有一台空闲的服务器,直接拿来安装centos.kvm等等,然后相关的配置. ...
- Linux系统升级更新openssh 7.3p1
放在最前面:鉴于网上爬虫猖獗,博客被盗时有发生,这里需要来个链接,大家请认准来自博客园的Scoter:http://www.cnblogs.com/scoter2008,本文将持续更新 最近绿盟给扫描 ...