【Leetcode解題記錄】17. Letter Combinations of a Phone Number

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:

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

--------

剛開始理解錯意思所以解這道題花的時間比較久。整體思路比較簡單。

public List<String> letterCombinations(String digits) {
		List<String> result = new ArrayList<String>();
        if(digits.length()==0)return result;
		result.add("");
		String[] de = {"","", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
		for (int i = 0; i < digits.length(); i++) {
			List<String> list = new ArrayList<String>();
			for (int j = 0; j <result.size(); j++) {
				for(int x = 0;x<de[digits.charAt(i)-'0'].length();++x){
					String s = result.get(j) + de[digits.charAt(i)-'0'].charAt(x);
					list.add(s);
				}
			}
			result=list;
		}
		return result;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章