[算法分析与设计] leetcode 每周一题: Word Ladder
2017-11-22 13:45
561 查看
题目链接:https://leetcode.com/problems/word-ladder/description/
题目 :
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
Only one letter can be changed at a time.
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord =
endWord =
wordList =
As one shortest transformation is
return its length
思路:
一开始我是用图的思想写此题,把每个字典里的字符串作为结点,两个字符串只有一个字母变化才有边,但是后来写不下去了,就看了别人博客,发现可以广度搜索解决。。。
大体上是这样的,首先为了减少每次查询字典的时间(如果每次都遍历vector<sting>,有点浪费时间),需要将字典词列表存储在set中,其次,维护一个map,key是字符串,value是广搜到这个字符串节点的路径长度,接着压入源字符广搜。
注意要点:
1. endWord极有可能是没办法生成的,如果endWord根本就不在字典里,所以需要当newWord == endWord 时就判断合法性,或者一开始就判断
2.存在无限广搜的情况,因为如果有解的话,路径中的节点必然不可能重复,如果压入节点时候,没有查重,就会无限广搜
代码如下:
class Solution {
public:
int ladderLength(string beginWord, str
4000
ing endWord, vector<string>& wordList) {
map<string, int>pathLen;// 记录每个点的路径耗散
pathLen[beginWord] = 1;
int len = beginWord.size(); // 记录字长
set<string> dict(wordList.begin(), wordList.end());// 字典查询变化后的word是否合法
queue<string> wordQueue;
wordQueue.push(beginWord);
stringstream ss;
while(!wordQueue.empty()) {
string word = wordQueue.front();
wordQueue.pop();
for(int i = 0; i < len; i++) {
string newWord = word;
for(char j = 'a' ;j <= 'z'; j++) {
if(word[i] != j) {
newWord[i] = j;
if(dict.find(newWord) != dict.end() && newWord == endWord) {
return pathLen[word] + 1;
}
if(dict.find(newWord) != dict.end() && pathLen.find(newWord) == pathLen.end()) {
pathLen[newWord] = pathLen[word] + 1;
wordQueue.push(newWord);
}
}
}
}
}
return 0;
}
};
题目 :
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
Only one letter can be changed at a time.
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord =
"hit"
endWord =
"cog"
wordList =
["hot","dot","dog","lot","log","cog"]
As one shortest transformation is
"hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length
5.
思路:
一开始我是用图的思想写此题,把每个字典里的字符串作为结点,两个字符串只有一个字母变化才有边,但是后来写不下去了,就看了别人博客,发现可以广度搜索解决。。。
大体上是这样的,首先为了减少每次查询字典的时间(如果每次都遍历vector<sting>,有点浪费时间),需要将字典词列表存储在set中,其次,维护一个map,key是字符串,value是广搜到这个字符串节点的路径长度,接着压入源字符广搜。
注意要点:
1. endWord极有可能是没办法生成的,如果endWord根本就不在字典里,所以需要当newWord == endWord 时就判断合法性,或者一开始就判断
2.存在无限广搜的情况,因为如果有解的话,路径中的节点必然不可能重复,如果压入节点时候,没有查重,就会无限广搜
代码如下:
class Solution {
public:
int ladderLength(string beginWord, str
4000
ing endWord, vector<string>& wordList) {
map<string, int>pathLen;// 记录每个点的路径耗散
pathLen[beginWord] = 1;
int len = beginWord.size(); // 记录字长
set<string> dict(wordList.begin(), wordList.end());// 字典查询变化后的word是否合法
queue<string> wordQueue;
wordQueue.push(beginWord);
stringstream ss;
while(!wordQueue.empty()) {
string word = wordQueue.front();
wordQueue.pop();
for(int i = 0; i < len; i++) {
string newWord = word;
for(char j = 'a' ;j <= 'z'; j++) {
if(word[i] != j) {
newWord[i] = j;
if(dict.find(newWord) != dict.end() && newWord == endWord) {
return pathLen[word] + 1;
}
if(dict.find(newWord) != dict.end() && pathLen.find(newWord) == pathLen.end()) {
pathLen[newWord] = pathLen[word] + 1;
wordQueue.push(newWord);
}
}
}
}
}
return 0;
}
};
相关文章推荐
- [算法分析与设计] leetcode 每周一题: Find Right Interval
- [算法分析与设计] leetcode 每周一题: 007. Reverse Integer
- [算法分析与设计] leetcode 每周一题: 135. Candy
- [算法分析与设计] leetcode 每周一题: Validate Binary Search Tree
- [算法分析与设计] leetcode 每周一题: 030. Substring with Concatenation of All Words
- [算法分析与设计] leetcode 每周一题: 467. Unique Substrings in Wraparound String
- [算法分析与设计] leetcode 每周一题: 420. Strong Password Checker
- [算法分析与设计] leetcode 每周一题: 335. Self Crossing
- [算法分析与设计] leetcode 每周一题: Minimum Time Difference
- [算法分析与设计] leetcode 每周一题:Longest Substring Without Repeating Characters
- [算法分析与设计] leetcode 每周一题: Surrounded Regions
- [算法分析与设计] leetcode 每周一题: 053. Maximum Subarray
- [算法分析与设计] leetcode 每周一题: 312. Burst Balloons
- [算法分析与设计] leetcode 每周一题: Kth Smallest Element in a BST
- [算法分析与设计] leetcode 每周一题: 310. Minimum Height Trees
- [算法分析与设计] leetcode 每周一题: 215. Kth Largest Element in an Array
- [算法分析与设计] leetcode 每周一题: 542. 01 Matrix
- [算法分析与设计] leetcode 每周一题: Set Matrix Zeroes
- [算法分析与设计] leetcode 每周一题: 667. Beautiful Arrangement II
- [算法分析与设计] leetcode 每周一题: 62. Unique Paths