zoukankan      html  css  js  c++  java
  • Java [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.

    解题思路:

    递归法解题之。从第一个字符串开始确认结果,然后递归地确认余下各项结果。

    参考博客:http://blog.csdn.net/china_wanglong/article/details/38495355

    代码如下:

    public class Solution {
        public List<String> letterCombinations(String digits) {
    		List<String> result = new ArrayList<String>();
    		String[] map = new String[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    		char[] tmp = new char[digits.length()];
    		if(digits.length() == 0)
    			return result;
    		rec(digits, 0, tmp, map, result);
    		return result;
    	}
    	public void rec(String digits, int index, char[] tmp, String[] map, List<String> result){
    		if(index == digits.length()){
    			result.add(new String(tmp));
    			return;
    		}
    		char tmpChar = digits.charAt(index);
    		for(int i = 0; i < map[tmpChar - '0'].length(); i++){
    			tmp[index] = map[tmpChar - '0'].charAt(i);
    			rec(digits, index + 1, tmp, map, result);
    		}
    	}
    }
    
  • 相关阅读:
    005本周总结报告
    《大道至简》读后感
    004本周总结报告
    003本周总结报告
    002本周总结报告
    001本周总结报告
    【财经】股市是不是零和博弈?
    【Spring】使用Spring的AbstractRoutingDataSource实现多数据源切换
    【Java每日一题】20170217
    【Java每日一题】20170216
  • 原文地址:https://www.cnblogs.com/zihaowang/p/4492032.html
Copyright © 2011-2022 走看看