-
Notifications
You must be signed in to change notification settings - Fork 0
/
127.单词接龙.cpp
55 lines (49 loc) · 1.34 KB
/
127.单词接龙.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
/*
* @lc app=leetcode.cn id=127 lang=cpp
*
* [127] 单词接龙
*/
// @lc code=start
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
class Solution {
public:
int ladderLength(std::string beginWord, std::string endWord, std::vector<std::string>& wordList) {
if(std::find(wordList.begin(), wordList.end(), endWord) == wordList.end())
return 0;
std::queue<std::pair<int, std::string>> q;
q.push({0, std::move(beginWord)});
while(!q.empty()){
auto& node = q.front();
for(auto it = wordList.begin(); it != wordList.end();){
if(canChange(node.second, *it)){
if(*it == endWord){
return node.first + 2;
}
q.push({node.first + 1, std::move(*it)});
it = wordList.erase(it);
}
else{
++it;
}
}
q.pop();
}
return 0;
}
private:
bool canChange(const std::string& s1, const std::string& s2) const{
int cnt = 0;
for(int i = 0;i < s1.size(); ++i){
if(s1[i] != s2[i]){
cnt++;
}
if(cnt > 1)
return false;
}
return cnt == 1;
}
};
// @lc code=end