-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path433-最小基因变化.cpp
More file actions
36 lines (36 loc) · 1.07 KB
/
433-最小基因变化.cpp
File metadata and controls
36 lines (36 loc) · 1.07 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
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
if (bank.empty()) return -1;
vector<char> gens{'A','C','G','T'};
unordered_set<string> s{bank.begin(), bank.end()};
unordered_set<string> visited;
queue<string> q{{start}};
int level = 0;
while (!q.empty())
{
int len = q.size();
for (int i = 0; i < len; ++i)
{
string t = q.front(); q.pop();
if (t == end) return level;
for (int j = 0; j < t.size(); ++j)
{
char old = t[j];
for (char c : gens)
{
t[j] = c;
if (s.count(t) && !visited.count(t))
{
visited.insert(t);
q.push(t);
}
}
t[j] = old;
}
}
++level;
}
return -1;
}
};