Skip to content

Commit 6a9aaa0

Browse files
committed
add js solution for leetcode 17
1 parent 3a17c48 commit 6a9aaa0

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ LeetCode
397397
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [js](./algorithms/validParentheses/validParentheses.js) |Easy|
398398
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [js](./algorithms/removeNthNodeFromEndOfList/removeNthNodeFromEndOfList.js) |Easy|
399399
|18|[4Sum](https://leetcode.com/problems/4sum/)| [js](./algorithms/4Sum/4Sum.js) |Medium|
400-
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| |Medium|
400+
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [js](./algorithms/letterCombinationsOfAPhoneNumber/letterCombinationsOfAPhoneNumber.js) |Medium|
401401
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| |Medium|
402402
|15|[3Sum](https://leetcode.com/problems/3sum/)| [js](./algorithms/3Sum/3Sum.js) |Medium|
403403
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [js](./algorithms/longestCommonPrefix/longestCommonPrefix.js) |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var letterCombinations = function (digits) {
2+
if (digits.length == 0) return [];
3+
4+
const map = new Map(),
5+
res = [];
6+
// 初始化字典映射
7+
map.set("2", "abc");
8+
map.set("3", "def");
9+
map.set("4", "ghi");
10+
map.set("5", "jkl");
11+
map.set("6", "mno");
12+
map.set("7", "pqrs");
13+
map.set("8", "tuv");
14+
map.set("9", "wxyz");
15+
16+
const dfs = (curStr, i) => {
17+
// 指针越界,递归的出口
18+
if (i > digits.length - 1) {
19+
// 将解推入res
20+
res.push(curStr);
21+
// 结束当前递归分支,进入另一个递归分支
22+
return;
23+
}
24+
25+
const letters = map.get(digits[i]);
26+
// 不同的字母选择代表不同的递归分支
27+
for (const l of letters) {
28+
dfs(curStr + l, i + 1);
29+
}
30+
};
31+
dfs("", 0);
32+
return res;
33+
};

0 commit comments

Comments
 (0)