Skip to content

Commit 67886c1

Browse files
committed
feat: add solutions to lc problem: No.0433
No.0433.Minimum Genetic Mutation
1 parent 80d0720 commit 67886c1

File tree

36 files changed

+433
-56
lines changed

36 files changed

+433
-56
lines changed

lcci/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [《程序员面试金典(第 6 版)》系列](https://leetcode-cn.com/problemset/lcci/)
1+
# 《程序员面试金典(第 6 版)》系列
22

33
本书是原谷歌资深面试官的经验之作,帮助了许多想要加入脸书、苹果、谷歌等 IT 名企的求职者拿到 Dream offer。本专题的 100+ 编程面试题是在原书基础上精心挑选出来的,帮助你轻松应战 IT 名企技术面试。
44

lcof/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [《剑指 Offer(第 2 版)》系列](https://leetcode-cn.com/problemset/lcof/)
1+
# 《剑指 Offer(第 2 版)》系列
22

33
![](./lcof.png)
44

lcof2/剑指 Offer II 023. 两个链表的第一个重合节点/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<p><strong>示例 1:</strong></p>
2020

21-
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_1.png" style="height: 130px; width: 400px;" /></a></p>
21+
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_1.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_1.png" style="height: 130px; width: 400px;" /></a></p>
2222

2323
<pre>
2424
<strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
@@ -30,7 +30,7 @@
3030

3131
<p><strong>示例&nbsp;2:</strong></p>
3232

33-
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_2.png" style="height: 136px; width: 350px;" /></a></p>
33+
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_2.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_2.png" style="height: 136px; width: 350px;" /></a></p>
3434

3535
<pre>
3636
<strong>输入:</strong>intersectVal&nbsp;= 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
@@ -42,7 +42,7 @@
4242

4343
<p><strong>示例&nbsp;3:</strong></p>
4444

45-
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_3.png" style="height: 126px; width: 200px;" /></a></p>
45+
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_3.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20023.%20%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%87%8D%E5%90%88%E8%8A%82%E7%82%B9/images/160_example_3.png" style="height: 126px; width: 200px;" /></a></p>
4646

4747
<pre>
4848
<strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2

lcof2/剑指 Offer II 054. 所有大于等于节点的值之和/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<p><strong>示例 1:</strong></p>
2222

23-
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/05/03/tree.png" style="height: 364px; width: 534px;" /></strong></p>
23+
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20054.%20%E6%89%80%E6%9C%89%E5%A4%A7%E4%BA%8E%E7%AD%89%E4%BA%8E%E8%8A%82%E7%82%B9%E7%9A%84%E5%80%BC%E4%B9%8B%E5%92%8C/images/tree.png" style="height: 364px; width: 534px;" /></strong></p>
2424

2525
<pre>
2626
<strong>输入:</strong>root<strong> </strong>=<strong> </strong>[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Loading
Binary file not shown.

lcof2/剑指 Offer II 077. 链表排序/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<p><strong>示例 1:</strong></p>
1515

16-
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 302px; " /></p>
16+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20077.%20%E9%93%BE%E8%A1%A8%E6%8E%92%E5%BA%8F/images/sort_list_1.jpg" style="width: 302px; " /></p>
1717

1818
<pre>
1919
<b>输入:</b>head = [4,2,1,3]
@@ -22,7 +22,7 @@
2222

2323
<p><strong>示例 2:</strong></p>
2424

25-
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 402px; " /></p>
25+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20077.%20%E9%93%BE%E8%A1%A8%E6%8E%92%E5%BA%8F/images/sort_list_2.jpg" style="width: 402px; " /></p>
2626

2727
<pre>
2828
<b>输入:</b>head = [-1,5,3,4,0]
Loading
Loading

solution/0400-0499/0433.Minimum Genetic Mutation/README.md

+129-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,150 @@ bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
BFS 找最值。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
<!-- 这里可写当前语言的特殊实现逻辑 -->
6870

6971
```python
70-
72+
class Solution:
73+
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
74+
s = set(bank)
75+
q = deque([(start, 0)])
76+
mp = {'A': 'TCG', 'T': 'ACG', 'C': 'ATG', 'G': 'ATC'}
77+
while q:
78+
t, step = q.popleft()
79+
if t == end:
80+
return step
81+
for i, v in enumerate(t):
82+
for j in mp[v]:
83+
next = t[:i] + j + t[i + 1:]
84+
if next in s:
85+
q.append((next, step + 1))
86+
s.remove(next)
87+
return -1
7188
```
7289

7390
### **Java**
7491

7592
<!-- 这里可写当前语言的特殊实现逻辑 -->
7693

7794
```java
95+
class Solution {
96+
public int minMutation(String start, String end, String[] bank) {
97+
Set<String> s = new HashSet<>();
98+
for (String b : bank) {
99+
s.add(b);
100+
}
101+
Map<Character, String> mp = new HashMap<>(4);
102+
mp.put('A', "TCG");
103+
mp.put('T', "ACG");
104+
mp.put('C', "ATG");
105+
mp.put('G', "ATC");
106+
Deque<Pair<String, Integer>> q = new LinkedList<>();
107+
q.offer(new Pair<>(start, 0));
108+
while (!q.isEmpty()) {
109+
Pair<String, Integer> p = q.poll();
110+
String t = p.getKey();
111+
int step = p.getValue();
112+
if (end.equals(t)) {
113+
return step;
114+
}
115+
for (int i = 0; i < t.length(); ++i) {
116+
for (char c : mp.get(t.charAt(i)).toCharArray()) {
117+
String next = t.substring(0, i) + c + t.substring(i + 1);
118+
if (s.contains(next)) {
119+
q.offer(new Pair<>(next, step + 1));
120+
s.remove(next);
121+
}
122+
}
123+
}
124+
}
125+
return -1;
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
int minMutation(string start, string end, vector<string>& bank) {
136+
unordered_set<string> s;
137+
for (auto& b : bank) s.insert(b);
138+
unordered_map<char, string> mp;
139+
mp['A'] = "TCG";
140+
mp['T'] = "ACG";
141+
mp['C'] = "ATG";
142+
mp['G'] = "ATC";
143+
queue<pair<string, int>> q;
144+
q.push({start, 0});
145+
while (!q.empty())
146+
{
147+
auto p = q.front();
148+
q.pop();
149+
string t = p.first;
150+
int step = p.second;
151+
if (t == end) return step;
152+
for (int i = 0; i < t.size(); ++i)
153+
{
154+
for (char c : mp[t[i]])
155+
{
156+
string next = t.substr(0, i) + c + t.substr(i + 1, t.size() - i - 1);
157+
if (s.count(next))
158+
{
159+
q.push({next, step + 1});
160+
s.erase(next);
161+
}
162+
}
163+
}
164+
}
165+
return -1;
166+
}
167+
};
168+
```
78169
170+
### **Go**
171+
172+
```go
173+
func minMutation(start string, end string, bank []string) int {
174+
s := make(map[string]bool)
175+
for _, b := range bank {
176+
s[b] = true
177+
}
178+
mp := make(map[byte]string)
179+
mp['A'] = "TCG"
180+
mp['T'] = "ACG"
181+
mp['C'] = "ATG"
182+
mp['G'] = "ATC"
183+
type pair struct {
184+
first string
185+
second int
186+
}
187+
q := []pair{{start, 0}}
188+
for len(q) > 0 {
189+
p := q[0]
190+
q = q[1:]
191+
t, step := p.first, p.second
192+
if t == end {
193+
return step
194+
}
195+
for i := 0; i < len(t); i++ {
196+
for _, c := range mp[t[i]] {
197+
next := t[:i] + string(c) + t[i+1:]
198+
if s[next] {
199+
q = append(q, pair{next, step + 1})
200+
s[next] = false
201+
}
202+
}
203+
}
204+
}
205+
return -1
206+
}
79207
```
80208

81209
### **...**

solution/0400-0499/0433.Minimum Genetic Mutation/README_EN.md

+129-1
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,146 @@
5353

5454
## Solutions
5555

56+
BFS.
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
```python
61-
63+
class Solution:
64+
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
65+
s = set(bank)
66+
q = deque([(start, 0)])
67+
mp = {'A': 'TCG', 'T': 'ACG', 'C': 'ATG', 'G': 'ATC'}
68+
while q:
69+
t, step = q.popleft()
70+
if t == end:
71+
return step
72+
for i, v in enumerate(t):
73+
for j in mp[v]:
74+
next = t[:i] + j + t[i + 1:]
75+
if next in s:
76+
q.append((next, step + 1))
77+
s.remove(next)
78+
return -1
6279
```
6380

6481
### **Java**
6582

6683
```java
84+
class Solution {
85+
public int minMutation(String start, String end, String[] bank) {
86+
Set<String> s = new HashSet<>();
87+
for (String b : bank) {
88+
s.add(b);
89+
}
90+
Map<Character, String> mp = new HashMap<>(4);
91+
mp.put('A', "TCG");
92+
mp.put('T', "ACG");
93+
mp.put('C', "ATG");
94+
mp.put('G', "ATC");
95+
Deque<Pair<String, Integer>> q = new LinkedList<>();
96+
q.offer(new Pair<>(start, 0));
97+
while (!q.isEmpty()) {
98+
Pair<String, Integer> p = q.poll();
99+
String t = p.getKey();
100+
int step = p.getValue();
101+
if (end.equals(t)) {
102+
return step;
103+
}
104+
for (int i = 0; i < t.length(); ++i) {
105+
for (char c : mp.get(t.charAt(i)).toCharArray()) {
106+
String next = t.substring(0, i) + c + t.substring(i + 1);
107+
if (s.contains(next)) {
108+
q.offer(new Pair<>(next, step + 1));
109+
s.remove(next);
110+
}
111+
}
112+
}
113+
}
114+
return -1;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
int minMutation(string start, string end, vector<string>& bank) {
125+
unordered_set<string> s;
126+
for (auto& b : bank) s.insert(b);
127+
unordered_map<char, string> mp;
128+
mp['A'] = "TCG";
129+
mp['T'] = "ACG";
130+
mp['C'] = "ATG";
131+
mp['G'] = "ATC";
132+
queue<pair<string, int>> q;
133+
q.push({start, 0});
134+
while (!q.empty())
135+
{
136+
auto p = q.front();
137+
q.pop();
138+
string t = p.first;
139+
int step = p.second;
140+
if (t == end) return step;
141+
for (int i = 0; i < t.size(); ++i)
142+
{
143+
for (char c : mp[t[i]])
144+
{
145+
string next = t.substr(0, i) + c + t.substr(i + 1, t.size() - i - 1);
146+
if (s.count(next))
147+
{
148+
q.push({next, step + 1});
149+
s.erase(next);
150+
}
151+
}
152+
}
153+
}
154+
return -1;
155+
}
156+
};
157+
```
67158
159+
### **Go**
160+
161+
```go
162+
func minMutation(start string, end string, bank []string) int {
163+
s := make(map[string]bool)
164+
for _, b := range bank {
165+
s[b] = true
166+
}
167+
mp := make(map[byte]string)
168+
mp['A'] = "TCG"
169+
mp['T'] = "ACG"
170+
mp['C'] = "ATG"
171+
mp['G'] = "ATC"
172+
type pair struct {
173+
first string
174+
second int
175+
}
176+
q := []pair{{start, 0}}
177+
for len(q) > 0 {
178+
p := q[0]
179+
q = q[1:]
180+
t, step := p.first, p.second
181+
if t == end {
182+
return step
183+
}
184+
for i := 0; i < len(t); i++ {
185+
for _, c := range mp[t[i]] {
186+
next := t[:i] + string(c) + t[i+1:]
187+
if s[next] {
188+
q = append(q, pair{next, step + 1})
189+
s[next] = false
190+
}
191+
}
192+
}
193+
}
194+
return -1
195+
}
68196
```
69197

70198
### **...**

0 commit comments

Comments
 (0)