• [LeetCode][Java] 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.

    题意:

    给定一个数字字符串,返回这个数字能代表的全部的字母的组合。

    数字对字母的映射例如以下图所看到的(就在电话的按键中)

    Input:Digit string "23"
    Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
    返回的结果能够依照随意的顺序。

    算法分析:

     * 思路:


     *比方“234”这个字符串,我能够先将0...1的全部排列找到-->{"a", "b", "c"}


     *再进一步将0...2的全部排列找到-->{"ad", "ae","af", "bd", "be", "bf", "cd", "ce", "cf"}


     *如此循环...直到字符串末尾。实现例如以下

    AC代码:

    public class Solution 
    {
        public  ArrayList<String> letterCombinations(String digits) 
        {
        	ArrayList<String> res=new ArrayList<String>();
        	String charmap[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        	if (digits.length()==0) return res;
        	res.add("");
            for (int i = 0; i < digits.length(); i++)
            {
              ArrayList<String> tempres=new ArrayList<String>(); 
              String chars = charmap[digits.charAt(i) - '0'];
              for (int c = 0; c < chars.length();c++)
              {
                for (int j = 0; j < res.size();j++)
                  tempres.add(res.get(j)+chars.charAt(c));
              }
                res = tempres;
            }
            return res;
        }
    }


  • 相关阅读:
    使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序
    算法7-4:宽度优先搜索
    R语言字符串函数
    notepad++ 正则表达式
    MySQL常用命令
    linux下对符合条件的文件大小做汇总统计的简单命令
    Linux系统下统计目录及其子目录文件个数
    R: count number of distinct values in a vector
    ggplot2 demo
    R programming, In ks.test(x, y) : p-value will be approximate in the presence of ties
  • 原文地址:https://www.cnblogs.com/llguanli/p/7202385.html
Copyright © 2020-2023  润新知