Given a digit string, 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.
Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路:使用递归算法,查表。
import java.util.ArrayList; import java.util.List; public class Solution { public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<String>(); if(digits == null || digits.length() == 0){ return result; } String[] map = new String[10]; map[0] = ""; map[1] = ""; map[2] = "abc"; map[3] = "def"; map[4] = "ghi"; map[5] = "jkl"; map[6] = "mno"; map[7] = "pqrs"; map[8] = "tuv"; map[9] = "wxyz"; char[] middleTemp = new char[digits.length()]; dfsGetStr(digits, 0, middleTemp, map, result); return result; } private void dfsGetStr(String digits, int index, char[] middleStr, String[] map, List<String> result) { if (index == digits.length()) { result.add(new String(middleStr)); return; } char strChar = digits.charAt(index); for (int i = 0; i < map[strChar - '0'].length(); i++) { middleStr[index] = map[strChar - '0'].charAt(i); dfsGetStr(digits, index + 1, middleStr, map, result); } } }