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; }