Skip to content

Commit 92c3963

Browse files
committed
17 621
1 parent ad027ba commit 92c3963

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Leetcode刷题记录
3535

3636
### dfs/bfs
3737

38+
+ No.17 [cpp](cpp/17.cpp)
3839
+ No.46 [python](python/46.py)
3940
+ No.126 [cpp](cpp/126.cpp) [python](python/126.py)
4041
+ No.543 [cpp](cpp/543.cpp) [python](python/543.py)
@@ -130,6 +131,7 @@ Leetcode刷题记录
130131

131132
+ No.1025 [python](python/1025.py)
132133
+ No.149 [cpp](cpp/149.cpp)
134+
+ No.621 [cpp](cpp/621.cpp)
133135

134136
## 剑指offer系列
135137

cpp/17.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<string> letterCombinations(string digits) {
8+
if (digits.size() == 0) return {};
9+
unordered_map<char, vector<char>> hash_map = {
10+
{'2', {'a', 'b', 'c'}},
11+
{'3', {'d', 'e', 'f'}},
12+
{'4', {'g', 'h', 'i'}},
13+
{'5', {'j', 'k', 'l'}},
14+
{'6', {'m', 'n', 'o'}},
15+
{'7', {'p', 'q', 'r', 's'}},
16+
{'8', {'t', 'u', 'v'}},
17+
{'9', {'w', 'x', 'y', 'z'}}
18+
};
19+
20+
vector<string> res;
21+
vector<char> temp;
22+
dfs(digits, 0, hash_map, temp, res);
23+
return res;
24+
25+
}
26+
void dfs(string &digits, int idx, unordered_map<char, vector<char>> &hash_map, vector<char> &temp,vector<string> &res)
27+
{
28+
int n = digits.size(), m = hash_map[digits[idx]].size();
29+
if (idx == n)
30+
res.emplace_back(temp.begin(), temp.end());
31+
32+
for (int i = 0; i < m; ++i) {
33+
temp.push_back(hash_map[digits[idx]][i]);
34+
dfs(digits, idx+1, hash_map, temp, res);
35+
temp.pop_back();
36+
}
37+
}
38+
};

cpp/621.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)