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.

解法一:深度优先解法

即backtracking方法,每次向下搜索直到触底。这种方法采用LIFO(后进后出),通过递归实现。

public class Solution {
public List<String> letterCombinations(String digits) {
LinkedList<String> rst = new LinkedList<String>();
if (digits == null || digits.length() == 0) {
return rst;
}
String[] mapping = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
String temp = "";
helper(rst, temp, digits, mapping, 0);
return rst;
}
public void helper(List<String> rst, String temp, String digits, String[] mapping, int index) {
if (index == digits.length()) {
rst.add(new String(temp));
return;
}
String s = mapping[Character.getNumericValue(digits.charAt(index))];
for (int i = 0; i < s.length(); i++) {
temp += s.charAt(i);
helper(rst, temp, digits, mapping, index + 1);
temp = temp.substring(0, temp.length() - 1);
}
}
}

解法二:广度优先解法

这种方法采用FIFO(先进先出),通过非递归方式实现。具体使用了LinkedList数据结构的add方法在list尾部插入,remove方法在list头部删除来实现FIFO。此外,还巧妙地运用了peek方法,每次新加元素时更新原有的每个结果。

public class Solution {
public List<String> letterCombinations(String digits) {
LinkedList<String> rst = new LinkedList<String>();
if (digits == null || digits.length() == 0) {
return rst;
}
String[] mapping = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
rst.add("");
for (int i = 0; i < digits.length(); i++) {
int m = Character.getNumericValue(digits.charAt(i));
while(rst.peek().length() == i) {
String s = rst.remove();
for (char c: mapping[m].toCharArray()) {
rst.add(s + c);
}
}
}
return rst;
}
}

值得思考的是,从直观上看第二种方法非递归应该运行效率更高,但实际两种方法的运行时间是一样的,都打败了46.02%的java submissions。

 

Letter Combinations of a Phone Number:深度优先和广度优先两种解法的更多相关文章

  1. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  2. 69. Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  3. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

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

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

  5. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...

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

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

  7. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  8. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  9. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

随机推荐

  1. selenium框架与chrome浏览器的不兼容问题

    在一次偶然的情况下,在chrome上用selenium框架去抓取某个id为XX的页面元素,使用WebDriver的findElement().click()方法进行点击,原来在firefox浏览器运行 ...

  2. 2月22日 《从Paxos到Zookeeper 分布式一致性原理与实践》读后感

    zk的特点: 分布式一致性的解决方案,包括:顺序一致性,原子性,单一视图,可靠性,实时性 zk的基本概念: 集群角色:not Master/Slave,is Leader/Follower/Obser ...

  3. Angular2 Service实践——实现简单音乐播放服务

    引言: 如果说组件系统(Component)是ng2应用的躯体,那把服务(Service)认为是流通于组件之间并为其带来生机的血液再合适不过了.组件间通信的其中一种优等选择就是使用服务,在ng1里就有 ...

  4. 部分linux系统命令(shell 命令)和hadoop命令

    linux系统命令(shell 命令): ls :  只列出文件/目录 ls -l :  会显示文件的详情,如大小等 ls -lh :  会显示文件的详情,但大小以k或者M为单位 ls ../ :  ...

  5. Mac入门推荐(写给Mac小白)

    本人第一次接触Mac是在2016年10月中旬,那时由于对苹果系统的不熟悉,导致自己一开始的时候用的很不习惯,甚至还想换回Windows系统.总所周知,苹果系统的软件比较少,在此我向大家推荐一些个人觉得 ...

  6. 线上问题debug过程(cat,grep,tr,awk,sort,uniq,comm等工具的综合使用)

    问题:发现线上到货单的数量,小于实际到货的数量. 怀疑一些隐藏的条件,将部分唯一码进行了过滤,导致数量变少. 开展了如下的跟踪流程: 1.找到其中一个明细的唯一码 grep 6180e-4b09f p ...

  7. Kubernetes运维生态-Heapster分析

    Heapster在Kubernetes的运维生态中如下:集群的容器的监控数据收敛汇聚层 heapster1.0版本后内部分为event和metric两个进程,可制作为两个docker镜像部署为两个独立 ...

  8. perl 正则表达式之漫游

    废话不多说了,直奔主题~ 一.简单模式进行匹配 将模式写在一对//中即可,匹配对象是$_ 元字符:圆括号()      进行模式分组 点号   .        能匹配任意一个字符,除换行符(\n)以 ...

  9. iOS 图片裁剪 + 旋转

    iOS 图片裁剪 + 旋转 之前分别介绍了图片裁剪和图片旋转方法 <iOS 图片裁剪方法> 地址:http://www.cnblogs.com/silence-cnblogs/p/6490 ...

  10. 2017-03-10 T-sql 语句 高级查询

    T-SQL语句: 创建数据库: 1,点击新建查询,在弹出的页面上进行代码编写.点击可用数据库,编写前确定当前操作的页面是自己想要进行操作的界面. 2,数据库创建语句 Create datebase   ...