leetcode 433. Minimum Genetic Mutation 和Word Ladder一样BFS + 反向链表 + DFS
来源:互联网 发布:永恒之柱 知乎 编辑:程序博客网 时间:2024/05/16 07:56
A gene string can be represented by an 8-character long string, with choices from “A”, “C”, “G”, “T”.
Suppose we need to investigate about a mutation (mutation from “start” to “end”), where ONE mutation is defined as ONE single character changed in the gene string.
For example, “AACCGGTT” -> “AACCGGTA” is 1 mutation.
Also, there is a given gene “bank”, which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.
Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from “start” to “end”. If there is no such a mutation, return -1.
Note:
Starting point is assumed to be valid, so it might not be included in the bank.
If multiple mutations are needed, all mutations during in the sequence must be valid.
You may assume start and end string is not the same.
Example 1:
start: “AACCGGTT”
end: “AACCGGTA”
bank: [“AACCGGTA”]
return: 1
Example 2:
start: “AACCGGTT”
end: “AAACGGTA”
bank: [“AACCGGTA”, “AACCGCTA”, “AAACGGTA”]
return: 2
Example 3:
start: “AAAAACCC”
end: “AACCCCCC”
bank: [“AAAACCCC”, “AAACCCCC”, “AACCCCCC”]
return: 3
题意很简单,和leetcode 126. Word Ladder II BFS + 反向链表 + DFS 和 leetcode 127. Word Ladder BFS广度优先遍历 一样的做法,建议一起学习
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;class Solution{public: int minMutation(string beginWord, string endWord, vector<string>& wordList) { set<string> wordSet(wordList.begin(), wordList.end()); if (wordSet.find(beginWord) != wordSet.end()) wordSet.erase(beginWord); queue<string> que; map<string, int> mp; int level = 1; que.push(beginWord); mp[beginWord] = level; while (que.empty() == false) { string now = que.front(); que.pop(); int level = mp[now]; for (int i = 0; i < now.length(); i++) { string tmp = now; string change = "ACGT"; for (char j : change) { tmp[i] = j; if (wordSet.find(tmp) != wordSet.end()) { if (tmp == endWord) return level; else { que.push(tmp); wordSet.erase(tmp); mp[tmp] = level + 1; } } } } } return -1; }};
- leetcode 433. Minimum Genetic Mutation 和Word Ladder一样BFS + 反向链表 + DFS
- LeetCode 433. Minimum Genetic Mutation
- [leetcode]433. Minimum Genetic Mutation
- leetcode 126. Word Ladder II BFS + 反向链表 + DFS
- 433. Minimum Genetic Mutation
- Minimum Genetic Mutation
- 126. Word Ladder II 、 127. Word Ladder(leetcode BFS+DFS)
- [leetcode] 126 Word Ladder ii bfs dfs
- LeetCode Word Ladder(BFS)
- leetcode word ladder bfs
- Word Ladder II(BFS + DFS)
- [leetcode]127 Word Ladder BFS
- 126. Word Ladder II(bfs+dfs)
- leetcode Word Ladder II hash BFS
- [C++]LeetCode: 130 Word Ladder (BFS)
- LeetCode Word Ladder II(BFS、SPFA)
- LeetCode *** 127. Word Ladder(BFS)
- Word Ladder,bfs
- JavaScript学习-Function类型
- POJ-1502 MPI Maelstrom
- 树形DP
- Android--文件压缩及文件解压
- 微信小程序开发系列(一) ——怎样开始进入小程序开发
- leetcode 433. Minimum Genetic Mutation 和Word Ladder一样BFS + 反向链表 + DFS
- 欢迎使用CSDN-markdown编辑器
- 水冷或风冷的数据中心?
- 架构模式 MVC、MVP、MVVM 的区别与联系
- Greenplum 清理垃圾、修改存储模式(行列变换) 平滑方法
- pcapy安装(windows)
- Python之memcache(二)
- 【Android】【问题处理】处理过的问题
- Git学习笔记