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

    Brute force method can be accepted by leetcode online judge.

    public class Solution {
        public ArrayList<String> letterCombinations(String digits) {
            String[] keyboard = new String[]{"","","abc","def","ghi","jkl","mno",
    												 "pqrs","tuv","wxyz"};
    		ArrayList<String> result = new ArrayList<String>();
    		result.add("");
    		for(int i = 0; i < digits.length(); ++i){
    			int k = digits.charAt(i) - 48;
    			if(k != 0 && k != 1){
    				ArrayList<String> temp = new ArrayList<String>();
    				while(!result.isEmpty()){
    					String s = result.remove(0);
    					for(int j = 0; j < keyboard[k].length(); ++j){
    						StringBuffer stb = new StringBuffer(s);
    						stb.append(keyboard[k].charAt(j));
    						temp.add(stb.toString());
    					}
    				}
    				result = temp;
    			}
    		}
    		return result;
        }
    }
    

      

  • 相关阅读:
    BZOJ3129/洛谷P3301方程(SDOI2013)容斥原理+扩展Lucas定理
    Dilworth定理
    字符串
    hash
    李超线段树(segment[HEOI2013]-洛谷T4097)
    连通数[JSOI2010]-洛谷T4306
    主席树
    splay
    树链剖分
    受欢迎的奶牛-洛谷2341
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3536934.html
Copyright © 2011-2022 走看看