[LeetCode] Word Ladder II 之一

// 算法:DFS

// 递归函数参数使用引用

// Time Limit Exceeded

 1 class Solution {
 2 public:
 3     vector< vector<string> > findLadders( string start, string end, unordered_set<string > &dict) {
 4
 5           vector< vector< string>  >  ret;
 6           vector<string> path;
 7                   if( start == end ) {
 8             path.push_back(start);
 9             ret.push_back(path);
10             return ret;
11                   }
12           else  if(dict.empty()){
13             return ret;
14            }
15            unordered_set<string> vis;
16            path.push_back(start);
17            vis.insert(start);
18            dfs(start,end,dict,path, vis, ret);
19
20            return ret;
21      }
22
23 private:
24 // 修改了函数参数
25      void  dfs( string start, string end, const unordered_set<string>& dict,
26                 vector<string>&  path, unordered_set<string>&  vis,vector<vector<string>  >&  ret )
27      {
28            size_t len = start.size();
29            for( int i = 0;i < len ;i++)
30            {
31                 for( char j = ‘a‘ ; j <= ‘z‘ ;j++)
32                 {
33
34                     if( j == start[i]){
35                         continue;
36                     }
37                     string  tmp = start;
38                     tmp[i] = j ;
39
40                     if( tmp == end) {
41                         path.push_back(end);
42                         if(  ret.empty()  || ret[0].size() == path.size() ){
43                             ret.push_back(path);
44                         }
45                         else if( ret[0].size() > path.size() )    {
46                             ret.clear();
47                             ret.push_back(path);
48                         }
49                         path.pop_back();
50
51                         return;
52
53                     }
54
55                     if (  dict.count(tmp) == 0 || vis.count(tmp) != 0 ) {
56                         continue;
57                     }
58
59                     path.push_back(tmp);
60                     vis.insert(tmp);
61
62                     dfs(tmp,end,dict,path,vis,ret);
63
64                     path.pop_back();
65                     vis.erase(tmp);
66
67                 }
68
69            }
70      }
71 };
72                

时间: 2024-12-31 03:50:52

[LeetCode] Word Ladder II 之一的相关文章

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

[python leetcode] Word Ladder II (very hard)[非常难,放弃]

Word Ladder II 描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: • Only one letter can be changed at a time • Each intermediate word must exist in the dictionary For examp

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

leetcode&mdash;&mdash;Word Ladder II

    改了3个晚上终于没有bug了...结果超时了...我已经想不出该怎么优化了....看了答案,是一层一层遍历,然后将每个单词的父节点push进一个vector里,我也是一层层,并且如果在下一层遇到上一层的某个单词,就记录单词被访问过,就不会再访问了...就是想不明白为啥用的时间要多很多...难道是因为我的代码不够简洁么?!!... class Solution { // 用广度搜索,建立由start开始的搜索树,子节点为父节点只改变一个字母在字典里能查到的所有的单词 // 每个子节点能记录

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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 =

126. Word Ladder II(js)

126. Word Ladder II Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in

18. Word Ladder &amp;&amp; Word Ladder II

Word Ladder 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,

LeetCode: Word Break II [140]

[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cat

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "c