-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.电话号码的字母组合.js
51 lines (48 loc) · 931 Bytes
/
17.电话号码的字母组合.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=17 lang=javascript
*
* [17] 电话号码的字母组合
*/
// @lc code=start
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function(digits) {
const mapping = [
'',
'',
'abc',
'def',
'ghi',
'jkl',
'mno',
'pqrs',
'tuv',
'wxyz'
]
const res = []
if (digits.length === 0) {
return res
}
backtrack(digits, 0, [])
return res
function backtrack(digits, start, sb = []) {
if (sb.length === digits.length) {
res.push(sb.join(''))
return
}
for (let i = start; i < digits.length; i++) {
const digit = Number(digits.charAt(i))
const str = mapping[digit]
for (let j = 0; j < str.length; j++) {
let c = str.charAt(j)
sb.push(c)
backtrack(digits, i + 1, sb)
sb.pop()
}
}
}
}
// @lc code=end
module.exports = { letterCombinations }