Skip to content

Commit

Permalink
添加(0017.电话号码的字母组合.md):增加typesript版本
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofei-2020 committed Mar 28, 2022
1 parent 1d19c5b commit 0296ac0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions problems/0017.电话号码的字母组合.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,40 @@ var letterCombinations = function(digits) {
};
```

## TypeScript

```typescript
function letterCombinations(digits: string): string[] {
if (digits === '') return [];
const strMap: { [index: string]: string[] } = {
1: [],
2: ['a', 'b', 'c'],
3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'],
5: ['j', 'k', 'l'],
6: ['m', 'n', 'o'],
7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z'],
}
const resArr: string[] = [];
function backTracking(digits: string, curIndex: number, route: string[]): void {
if (curIndex === digits.length) {
resArr.push(route.join(''));
return;
}
let tempArr: string[] = strMap[digits[curIndex]];
for (let i = 0, length = tempArr.length; i < length; i++) {
route.push(tempArr[i]);
backTracking(digits, curIndex + 1, route);
route.pop();
}
}
backTracking(digits, 0, []);
return resArr;
};
```

## C

```c
Expand Down

0 comments on commit 0296ac0

Please sign in to comment.