【Leetcode】17. Letter Combinations of a Phone Number

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.


1 建立map的时候,key是数字,但也写成字符形式,因为输入进来的数字是字符串形式

2 使用recursive的方式,这是一个好hint,string和list也是可以用recursive的,每次都把最后一个分离出来,然后递归下去

3 返回的时候把前面部分和最后一个字符连接起来就行了

4 可以把输入digits的长度按0,1和多个来分类处理

5 题意说了不含‘0’和‘1’,所以我们可以不用去考虑这两种情况

6 当输入digits长度是1的时候,我们直接list输入digits对应的字母就行了

7 last = dic[digits[-1]] 最后这个直接mapping就行啦,我居然还去recursive,也是笨啊


当digits为空时,返回[],不是["]

所以递归的时候回,要注意返回回来的是不是空


TC:

SC: 


你可能感兴趣的:(【Leetcode】17. Letter Combinations of a Phone Number)