forked from algorithm023/algorithm023
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathladderLength_127.cpp
More file actions
41 lines (40 loc) · 1.4 KB
/
Copy pathladderLength_127.cpp
File metadata and controls
41 lines (40 loc) · 1.4 KB
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
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int size = wordList.size(), path = 0;
queue<string> q;
unordered_set<string> wordset(wordList.begin(), wordList.end()); //转化成哈希表,提高查找效率
if (wordset.find(endWord) == wordset.end()) return 0; //无法转化
unordered_map<string, bool> is_visited;
is_visited[beginWord] = 1;
q.push(beginWord);
++path;
while (!q.empty())
{
int qsize = q.size();
bool can_add = 0;
for (int i = 0; i < qsize; ++i)
{
int tmpsz = q.front().size();
for (int j = 0; j < tmpsz; ++j)
{
string tmp = q.front();
for (int alpha = 0; alpha < 26; ++alpha)
{
tmp[j] = 'a'+ alpha;
if (tmp == endWord) return path + 1;
if (is_visited[tmp] == 0 && wordset.find(tmp) != wordset.end())
{
is_visited[tmp] = 1;
q.push(tmp);
can_add = 1;
}
}
}
q.pop();
}
if (can_add) ++path;
}
return 0;
}
};