Given a digit string, return all possible letter combinations that the number could represent.
mapping of digit to letters (just like on the telephone buttons) is given below.
![]()
这道题要求咱们给出,对于输入的按键组合,咱们须要返回按键所对应的全部可能的字符串。而按键和字母的对应关系如上图。git
public List<String> letterCombinations(String digits) { LinkedList<String> res = new LinkedList<String>(); if(digits.length() == 0){ return res; } String[] mapping = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; res.add(""); for(int i=0;i<digits.length();i++){ int index = Character.getNumericValue(digits.charAt(i)); while(res.peek().length() == i){ String temp = res.remove(); for(char c : mapping[index].toCharArray()){ res.add(temp+c); } } } return res; }