码迷,mamicode.com
首页 > 其他好文 > 详细

17. Letter Combinations of a Phone Number

时间:2017-07-25 22:50:24      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:class   linked   dig   date   har   oid   letter   add   string   

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.

临时容器 可以是"", 结果容器, 位置, cadidates[],

此题考察要递归和遍历的地方, 要遍历的容器在变

private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    
    	public List<String> letterCombinations(String digits) {
    		List<String> ret = new LinkedList<String>();
    		if (digits == null || digits.length() == 0) return ret;
    		combination("", digits, 0, ret);
    		return ret;
    	}
    
    	private void combination(String prefix, String digits, int offset, List<String> ret) {
    		if (offset >= digits.length()) {
    			ret.add(prefix);
    			return;
    		}
    		String letters = KEYS[(digits.charAt(offset) - ‘0‘)];  //不断变化的, charAt() – ‘0’表示数组的位置
    		for (int i = 0; i < letters.length(); i++) {
    			combination(prefix + letters.charAt(i), digits, offset + 1, ret);
    		}
    	}
String letters = KEYS[(digits.charAt(offset) - ‘0‘)];  //不断变化的, charAt() – ‘0’表示数组的位置

  

17. Letter Combinations of a Phone Number

标签:class   linked   dig   date   har   oid   letter   add   string   

原文地址:http://www.cnblogs.com/apanda009/p/7236382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!