File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } digits
3+ * @return {string[] }
4+ */
5+ let letterMap = [
6+ " " , //0
7+ "" , //1
8+ "abc" , //2
9+ "def" , //3
10+ "ghi" , //4
11+ "jkl" , //5
12+ "mno" , //6
13+ "pqrs" , //7
14+ "tuv" , //8
15+ "wxyz" , //9
16+ ]
17+
18+ let letterCombinations = function ( digits ) {
19+ let res = [ ]
20+
21+ if ( digits === "" ) {
22+ return res
23+ }
24+
25+ /**
26+ *
27+ * @param {number } index 当前处理到的下标位置
28+ * @param {string } str 当前已经凑成的字符串
29+ */
30+ let findCombinations = ( index , str ) => {
31+ if ( digits . length === index ) {
32+ res . push ( str )
33+ return
34+ }
35+
36+ let char = digits [ index ] // 数字
37+ let letters = letterMap [ Number ( char ) ] // 数字对应的字母
38+
39+ for ( let i = 0 ; i < letters . length ; i ++ ) {
40+ let letter = letters [ i ]
41+ findCombinations ( index + 1 , `${ str } ${ letter } ` )
42+ }
43+ }
44+
45+ findCombinations ( 0 , "" )
46+
47+ return res
48+ }
You can’t perform that action at this time.
0 commit comments