题目:最长公共前缀

难度: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. 《挑战程序设计竞赛》2.5 最短路 AOJ0189 2249 2200 POJ3255 2139 3259 3268(5)

    AOJ0189 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189 题意 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  2. iOS中nil 、NULL、 Nil 、NSNull

    nil,定义一个空的实例,指向OC中对象的空指针. 示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = ni ...

  3. Eclipse打包java工程

    Eclipse打包java工程步骤如下: 1.选择预打包的工程->Export. 2.选择java->JAR file. 3.导出JAR文件设置. 这里有几个选项: Export gene ...

  4. 流畅的python 对象引用 可变性和垃圾回收

    对象引用.可变性和垃圾回收 变量不是盒子 人们经常使用“变量是盒子”这样的比喻,但是这有碍于理解面向对象语言中的引用式变量.Python 变量类似于 Java 中的引用式变量,因此最好把它们理解为附加 ...

  5. spring 整合mybatis找不到${jdbc.driverClass}

    1.检查是否设置了mapper扫描org.mybatis.spring.mapper.MapperScannerConfigurer类 在spring里使用org.mybatis.spring.map ...

  6. centos Docker安装前升级内核3.10的方法

    首先我虚拟机系统都是Centos 6.5 .ESXI ,后安装devel .ESXI 后来 .ESXI 我所操作的都是虚拟机,但是在真实机上面如何我就不清楚了~~ 大家一定要记得安装步骤,,,不然就是 ...

  7. [ngClass]、[ngStyle]的基本使用(转载)

    1.ngStyle 基本用法 <div [ngStyle]="{'background-color':'green'}"></<div> 判断添加 & ...

  8. Django项目在nginx上面的部署(django+flup+nginx)

    首先说下需要用到的软件1.python环境因为我用的centos6.2 上面自带了python2.6.6的环境.所以就可以不用安装了.2.Django环境我们这里用easy_install的方式来安装 ...

  9. NSIS卸载后无法删除开始菜单中的内容

    我们在安装程序时通常会使用createShortCut命令来创建一个快捷方式,如下,是在开始–>启动项 里创建run.bat的快捷方式. CreateShortCut "$SMPROG ...

  10. mybatis参数处理 $#