题目:最长公共前缀

难度:EASY

题目内容

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

翻译:给定一个包含2-9包含数字的字符串,返回所有可能表示的字母组合。

数字到字母的映射(就像电话上的按钮一样)如下所示。注意,1没有映射到任何字母。

Example:

Input: "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.

我的思路:首先要知道要做什么,再思考用什么数据结构(list、栈、队列、堆、map、set),再思考用什么算法。不要冲上来就想着算法。

    做什么,用什么做:根据输入的数字进行所有可能搭配,数据结构中每一个都要和新来的几个字符进行组合字符,而且新来字符组合数目并不确定,所以得采用while循环,并且每一个变成新的组合后又得回到原来的数据结构,队列结构就能完美解决。

    怎么做:首先弄一个空队列,for对digits循环,看队头,看它的长度是否已经是 i 个(for内),是则队头出队,再取digit对应的几个字符分别加在此字符串(队头)的后面。以此类推。

我的代码:

     public List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<String>();
if(digits.isEmpty()) return ans;
String[] mapping = new String[] {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add("");
for(int i = 0; i < digits.length(); i++){
int x = Integer.parseInt(digits.charAt(i)+"");
while(ans.peek().length() == i){
String t = ans.poll();
for(char s : mapping[x - 2].toCharArray())
ans.offer(t+s);
}
}
return ans;
}

时间复杂度:O(3n  +。。。+ 3 ) ≈ O(3n  )    3是每个按键大概是3个。 n是digits的长度。

答案

啊哈哈和我几乎一样,就不贴出来了~

就是在从字符转int的时候它使用的是:  int x = Character.getNumericValue(digits.charAt(i));  不知道Chracter有这个方法,记下了。

LeetCode第[17]题(Java):Letter Combinations of a Phone Number的更多相关文章

  1. [LeetCode][Java] Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  2. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  3. 【Leetcode】【Medium】Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  5. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  6. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  7. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 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 ...

  9. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

随机推荐

  1. "零代码”开发B/S企业管理软件之一 :怎么创建数据库表

    声明:该软件为本人原创作品,多年来一直在使用该软件做项目,软件本身也一直在改善,在增加新的功能.但一个人总是会有很多考虑不周全的地方,希望能找到做同类软件的同行一起探讨. 本人文笔不行,能把意思表达清 ...

  2. javaweb项目中嵌入webservice--axis2

    由于最近项目中需要搭建webservice服务端,由于原项目是javaweb项目,所以需要整合.之前用cxf试了,启动老是报错,maven依赖冲突.后来索性换成axis2 百度了一圈,下面这个博客 h ...

  3. pycharm调试scrapy

    pycharm调试scrapy 创建一个run.py文件作为调试入口 run.py中,name是要调试的爬虫的名字(注意,是爬虫类中的name,而不是爬虫类所在文件的名字) 拼接爬虫运行的命令,然后用 ...

  4. IIS设置文件 Robots.txt 禁止爬虫

    robots.txt用于禁止网络爬虫访问网站指定目录.robots.txt的格式采用面向行的语法:空行.注释行(以#打头).规则行.规则行的格式为:Field: value.常见的规则行:User-A ...

  5. SQL Server中行列转换 Pivot UnPivot

    PIVOT用于将列值旋转为列名(即行转列),在SQLServer 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列)FOR 列 in (-) )AS P 完 ...

  6. Spring-Hello World实例

    Spring Hello World实例 创建Java项目 添加Jar包 创建源文件 现在在Spring项目下创建实际的源文件.首先,要创建一个名为com.tuorialsponit的包,然后在该co ...

  7. Keras网络层之卷积层

    卷积层 Cov1D层 keras.layers.convolutional.Conv1D(filters, kernel_size, strides=1, padding='valid', dilat ...

  8. FPGA电源设计

    LDO(低压差线性稳压器),FPGA需要3.3V.2.5V和1.2V,可选用凌力尔特LINEAR:LT1083/84/85,低压差正压可调稳压器. 应用电路如图所示: 输入端加10UF电解电容,输出端 ...

  9. 吴超老师课程--HBASE的查询手机项目

    查询1.按RowKey查询2.按手机号码查询3.按手机号码的区域查询 //查询手机13450456688的所有上网记录 public static void scan(String tableName ...

  10. GIT学习笔记(4):远程分支

    GIT学习笔记(4):远程分支 远程分支 远程分支是什么 远程分支是对远程仓库中的分支的索引.它们是一些无法移动的本地分支:只有在GIT进行网络交互时才会更新.远程分支就是书签,提醒着你上次连接远程仓 ...