题目:最长公共前缀

难度: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. delphi xe -芒果数据库(FDConnection,DataSource,FDMongoQuery,FDMongoDataSet)连接,查询(展示数据),这里有mongodb为例子

    一.连接 1.FDConnection:创建一个临时连接定义 资料:http://www.cnblogs.com/zhenfei/p/4105515.html 连接芒果数据库:选则Mongo(芒果), ...

  2. delphi,数据类型,字符、浮点、整数、数组

    字符型:string 浮点型:real 整数:integer DELPHI的浮点数声明不是用float,而是用real(8个字节),single(8个字节,单精度浮点),double(16个字节,双精 ...

  3. visual studio 下 C++生成dump文件

    1 lib配置 项目-->属性-->配置属性-->链接器-->输入-->附加依赖项 增加dbghelp.lib 2 头文件 #include <imagehlp.h ...

  4. 11.css定义下拉菜单

    注意点: 1.设置a标签的width 和 height 的时候,直接设置是没用的,可以以这样两种方式设置 (1). display:block; (2). float:left; 2.设置下拉菜单,最 ...

  5. IIS 部署WCF时遇到这么个错:

    转(http://blog.csdn.net/vic0228/article/details/48806405) 部署WCF时遇到这么个错: "The service cannot be a ...

  6. BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶

    BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...

  7. tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py

    具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子:  ...

  8. Linux环境配置全局jdk和局部jdk并生效

    全局jdk配置: 1.root用户登录 2.进入opt目录,新建java文件夹 cd  /opt mkdir java  上传jdk7u79linuxx64.tar.gz包到java文件夹并解压 jd ...

  9. List集合的ForEach扩展

           public static void ForEach<T>(this IEnumerable<T> enumerableSource, Action<T&g ...

  10. Spark --idea无法new scala class

    问题: 无法新建Scala class 解决: 1.下载插件 setting-->Plugins-->安装scala插件-->提示重启idea-->自动提示你安装scala s ...