Skip to content

Commit

Permalink
add: 电话号码中的字符组合
Browse files Browse the repository at this point in the history
  • Loading branch information
xldeng committed Mar 5, 2022
1 parent c284520 commit 6d8733e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
2. [校验括号的合法性](https://github.com/dengshasha/algorithm-study/blob/master/string/validParentheses.js) || [leetcode地址](https://leetcode.com/problems/valid-parentheses/submissions/)
3. [在字符串中查找数组中的级联子串](https://github.com/dengshasha/algorithm-study/blob/master/string/substrWithConcatenation.js) || [leetcode地址](https://leetcode.com/problems/substring-with-concatenation-of-all-words/submissions/)
4. [实现atoi函数](https://github.com/dengshasha/algorithm-study/blob/master/string/strToInteger.js) || [leetcode地址](https://leetcode.com/problems/string-to-integer-atoi/submissions/)
5. [电话号码中的字符组合](https://github.com/dengshasha/algorithm-study/blob/master/string/letterCombOfPhoneNum.js) || [leetcode地址](https://leetcode.com/problems/letter-combinations-of-a-phone-number/submissions/)

### 数组
1. [数组各项,除该项外累乘的结果](https://github.com/dengshasha/algorithm-study/blob/master/array/productArrayExceptSelf.js) || [leetcode地址](https://leetcode.com/problems/product-of-array-except-self/submissions/)
Expand Down
4 changes: 0 additions & 4 deletions array/validSudoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,5 @@ var test1 = [
[".","2",".","9",".",".",".",".","."],
[".",".","4",".",".",".",".",".","."]]

// . . . | . 5 . | . 1 .
// . 4 . | 3 . . | . . .
// . . . | . . 3 | . . 1


console.log(isValidSudoku(test1))
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>Document</title>
</head>
<body>
<script src="interview/sortColors.js"></script>
<script src="string/letterCombOfPhoneNum.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions string/letterCombOfPhoneNum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {string} digits
* @return {string[]}
*/

const enums = {
2: 'abc',
3: 'def',
4: 'ghi',
5: 'jkl',
6: 'mno',
7: 'pqrs',
8: 'tuv',
9: 'wxyz'
}
var letterCombinations = function(digits) {
let charsList = getChars(digits) // ['abc', 'def']
let ans = []
recursive(0, "")

function recursive(i, str) {
if(str.length === charsList.length) {
ans.push(str)
return;
}
for(let k = 0; k < charsList[i].length; k++) {
recursive(i+1, str.concat(charsList[i][k]))
}

}
function getChars(digits) {
let charsList = []
for(let i = 0; i < digits.length; i++) {
charsList.push(enums[digits[i]])
}
return charsList
}
return ans;
};

console.log(letterCombinations('234'))

0 comments on commit 6d8733e

Please sign in to comment.