17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Numberarrow-up-right

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.

Example:

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

My Solutions:

先把电话号码盘存入String[] disc。0 --> 空格,1 --> 空string,2 --> "abc", ...

和combination类似,temp加入list的条件是temp长度和digits长度相同。

类似的,也可以用HashMap储存键盘。index也可以省略掉,因为可以用 letters记录每个数字对应的几个字母,用letters.toCharArray() 遍历,通过每次dfs时减少digits的长度,每一次letters取digits第一个数字上的字母。

Time & Space: O(3^M * 4^N), M是有3个字母的按键数量,N是有4个字母的按键数量

Last updated