代码拉取完成,页面将自动刷新
// Time: O(n * b), n is the length of gene string, b is size of bank
// Space: O(b)
class Solution {
public:
int minMutation(string start, string end, vector<string>& bank) {
unordered_map<string, bool> lookup;
for (const auto& b : bank) {
lookup.emplace(b, false);
}
queue<pair<string, int>> q;
q.emplace(start, 0);
while (!q.empty()) {
string cur;
int level;
tie(cur, level) = q.front(); q.pop();
if (cur == end) {
return level;
}
for (int i = 0; i < cur.size(); ++i) {
auto cur_copy = cur;
for (const auto& c : {'A', 'T', 'C', 'G'}) {
if (cur_copy[i] == c) {
continue;
}
cur_copy[i] = c;
if (lookup.count(cur_copy) && lookup[cur_copy] == false) {
q.emplace(cur_copy, level + 1);
lookup[cur_copy] = true;
}
}
}
}
return -1;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。