[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;
}
}
[LeetCode][Java] Letter Combinations of a Phone Number的更多相关文章
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
随机推荐
- Python day4知识回顾
# -*- coding: utf_8 _*_# Author:Vi#字典是无序的 info = { 'student001':"DIO", 'student002':" ...
- 【Pycharm】【HTML/jQuery】代码换行问题
问题:从网上下载jQuery文件后发现代码太长,不利于阅读:Pycharm没有预先设置好js文件的自动换行设置 问题 解决办法 解决后
- java.lang.ClassNotFoundException: org.springframework.web.content.ContextLoaderListener
1.错误描写叙述 严重: Error configuring application listener of class org.springframework.web.content.Context ...
- 9.使用 npm 命令安装模块
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html npm 安装 Node.js 模块语法格式如下: $ npm install <Modu ...
- HDU 2333 Assemble(二分)
Assemble Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- weblogic安装(无界面静默安装)
一.环境准备 1. 用户准备 Generic通用版weblogic不能用ROOT用户安装,如无其他用户需先创建用户,创建用户步骤此处略过 2. 下载weblogic 在官网下载weblogic,将下载 ...
- Docker+Solr
原文:Docker+Solr docker 内的solr并不是部署在tomcat里,而是自启动的.默认的home是/opt/solr/server/solr # docker search solr ...
- jsp中标签id和name的区别(转)
name原来是为了标识之用,但是现在根据规范,都建议用id来标识元素. 但是name在以下用途是不能替代的:1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多na ...
- MDaemon and Apache2
MDaemon and Apache2 邮件服务器列表 http://down.chinaz.com/class/93_1.htm MDaemon11.03中文破解补丁 http://download ...
- ie为什么那么垃圾(不是ie垃圾,是ie用的人太多了,很多在用低版本)
ie为什么那么垃圾(不是ie垃圾,是ie用的人太多了,很多在用低版本) 一.总结 1.我们觉得ie差的原因:我们拿老的ie和最新的其它浏览器做比较了,两者相差了很多年.比较微软几十年才发布了11个ie ...