-
Notifications
You must be signed in to change notification settings - Fork 0
/
126.cpp
111 lines (107 loc) · 3.5 KB
/
126.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Solution 1 : DFS
* time out
*/
class Solution {
public:
void backtracking(string now_s, string target, unordered_map<string, bool> &words, vector<vector<string> > &res, vector<string> local)
{
if(now_s == target)
{
local.push_back(target);
// 取最小长度的结果
while(!res.empty() && res[res.size() - 1].size() > local.size())
res.pop_back();
if(res.empty() || res[res.size() - 1].size() == local.size())
res.push_back(local);
return;
}
int len = now_s.size();
local.push_back(now_s);
for(int i = 0; i < len; i++)
{
for(char tem = 'a'; tem <= 'z'; tem++)
{
string tem_s = now_s;
tem_s[i] = tem;
auto ret = words.find(tem_s);
// 不在给定的路径里面 或者 之前被使用过
if(ret == words.end() || ret->second == false)
continue;
ret->second = false;
backtracking(tem_s, target, words, res, local);
ret->second = true;
}
}
}
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
vector<vector<string> > res;
vector<string> local;
unordered_map<string, bool> unused;
for(auto it : wordList)
unused.insert(make_pair(it, true));
backtracking(beginWord, endWord, unused, res, local);
return res;
}
};
/*
* Solution : BFS
* AC
*/
class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
// 所有单词的集合
unordered_set<string> words;
for(auto it : wordList) words.insert(it);
vector<vector<string> > res;
// 使用队列进行BFS
queue<vector<string> > paths;
paths.push({beginWord});
int level = 1;
int min_level = INT_MAX;
// 在本层中使用过的单词
vector<string> used;
while(!paths.empty())
{
vector<string> path = paths.front();
paths.pop();
// 遍历到新的层上
if(path.size() > level)
{
// 对上一层使用过的单词进行清理
for(string item : used) words.erase(item);
used.clear();
if(path.size() > min_level)
break;
else
level = path.size();
}
// 根据最后一个单词向后扩展
string last = path.back();
for(int i = 0; i < last.size(); i++)
{
string new_s = last;
for(char j = 'a'; j <= 'z'; j++)
{
new_s[i] = j;
// 是路径中的单词
if(words.find(new_s) != words.end())
{
vector<string> new_path = path;
new_path.push_back(new_s);
used.push_back(new_s);
if(new_s == endWord)
{
min_level = level;
res.push_back(new_path);
}
else
paths.push(new_path);
}
}
}
}
return res;
}
};