题目:最长公共前缀

难度: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. MVC异步消息推送机制

    在MVC里面,有异步控制器,可以实现模拟消息推送机制功能 1.控制器要继承至AsyncController,如 public class RealTimeController : AsyncContr ...

  2. 多个StoryBoard之间的跳转

    iOS项目中可以将同一业务流程的页面归置到一个StoryBoard中,项目中必然会包含多个StroryBoard,可以利用跳转,实现项目的不同业务流程页面间的跳转切换. 实现思路: 1,项目(Proj ...

  3. runtime(二)

    前言 上一篇中我们大致的了解了runtime的一些基本概念,这一篇我们一起来看看如何使用它. 3.如何使用runtime. 3.1 方法交换 举一个老生常谈的例子.当你接手一个新的项目,需要查看这个程 ...

  4. IIS网站部署解决报错

    入坑2次,这次还是得马上总结起来== 部署网站报以上错 检查方法 步骤一:检查部署的网站路径是否正确 步骤二: 检查Internet信息管理器中,应用程序池的.net Framework版本,选择v4 ...

  5. Piggy-Bank---hdu1114(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意是有一个存钱罐,当它是空的时候重量为E,满的时候重量为F:已知存钱罐里面有 n 种钱,每种钱 ...

  6. [译转]深入理解LayoutInflater.inflate()

    原文链接:https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ 译文连接:http://bl ...

  7. node.js---sails项目开发(3)

    1.为新建的sails数据库新建一个用户,首先连接数据库 mongo localhost:27017 (1)显示所有数据库   (2)切换数据库 show dbs use sails 新建一个用户 账 ...

  8. 图形数据库 Neo4j 开发实战【转载】

    简介: Neo4j 是一个高性能的 NoSQL 图形数据库.Neo4j 使用图(graph)相关的概念来描述数据模型,把数据保存为图中的节点以及节点之间的关系.很多应用中数据之间的关系,可以很直接地使 ...

  9. 用Tchromium替换webbrowser

    用Tchromium替换webbrowser 用惯了EmbeddedWB,不想换,但是IE内核一直存在内存泄漏问题,没办法,只有寻找替代品了. 要把用习惯的EmbeddedWB换成完全不一样的TChr ...

  10. kubeadm方式安装kubernetes

    系统: Ubuntu 18.04.2 LTS 内存: 8G 机器: 属性 IP Hostname ssh  Master  192.168.91.48 blackray-pc     node1  1 ...