题目:
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"].Note:
思路:
//digits-号码串 i-号码串的下标 temp-临时字符串 res-返回值 public void dfs(String digits, int i, String temp, List<String> res)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class LetterCombinations {
	HashMap<Integer, String> map = new HashMap<Integer, String>(){
		{
			put(0, " ");
			put(1, "");
			put(2, "abc");
			put(3, "def");
			put(4, "ghi");
			put(5, "jkl");
			put(6, "mno");
			put(7, "pqrs");
			put(8, "tuv");
			put(9, "wxyz");
		}
	};
	
	public List<String> letterCombinations(String digits) {
		
		List<String> res = new ArrayList<>();
		dfs(digits, 0, "", res);
		return res;
    }
	
	//digits-号码串    i-号码串的下标    temp-临时字符串    res-返回值
	public void dfs(String digits, int i, String temp, List<String> res){
		
		if(i == digits.length()){
			res.add(temp);
			return;
		}
		String s = map.get(digits.charAt(i)-'0');
		
		for(int j=0; j< s.length(); j++){
			String temp2 = temp +s.charAt(j);
			dfs(digits, i+1, temp2, res);
		}
	}
}
LeetCode | #17 Letter Combinations of a Phone Number
原文:http://blog.csdn.net/allhaillouis/article/details/43092219