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. windows下读取utf-8文件

    #include <stdio.h> #include <tchar.h> #include <memory> int main() { FILE* fp1 = f ...

  2. OpenCv 2.4.9 (一) Mat基础结构&如何遍历图片

    前言 因为对图像方面感兴趣,所以有空学学OpenCV的使用,并且希望以此为引子,带领自己入门图像领域. 先post上几个参考网站,上面有完整源码: http://docs.opencv.org/2.4 ...

  3. XML与JavaScript知识

    什么是XMLHttpRequest 对象? 答:XMLHttpRequest 对象用于在后台与服务器交换数据,它是开发者的梦想,因为它能够:1.在不重新加载页面的情况下更新网页:2.在页面已加载后从服 ...

  4. Linux Tomcat安装,Linux配置Tomcat,Linux Tomcat修改内存,Linux tomcat修改端口

    Linux Tomcat安装,Linux配置Tomcat,Linux Tomcat修改内存,Linux tomcat修改端口 >>>>>>>>>& ...

  5. 每天一个Linux命令(03)--pwd

    linux 中用 pwd命令来查看“当前工作目录”的完整路径.简单地说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判断当前目录在文件系统内的确切位置. ...

  6. Ubuntu 小白安装血泪史

    介绍: 新入手的Ubuntu:版本 命令行模式下 出现 ♦♦♦♦ 图形界面无法获取最高权限:gurb.cfg 无法再图形界面下修改 安装类型: 1.安装Ubuntu,与Window 7共存 2.清除整 ...

  7. HTML重要标签及属性详解

    我学习前端的时间不长,短短1个月而已,只学了些HTML5和CSS3还有少许javascript,另外还有网页布局等等辅助性书籍,我在模仿网页以及完成百度前端技术学院的任务过程中发现了我容易忘记的标签以 ...

  8. solr笔记之solr下载及安装

    在学习solr过程中,磕磕碰碰,遇到过许多问题,所以特写下笔记,以供需要的时候时常翻阅,也给能看到该博文的博友提供一个不全面的参考. 一.solr简介: Solr是一个独立的企业及搜索应用服务器,它对 ...

  9. css3滚动效果

    .css{ -webkit-transition-duration: .3s;    transition-duration: .3s; }

  10. Java 注解 入门

    这几天在学习Spring3.x,发觉现在许多框架都用上了java注解功能,然后自己就对java注解这方面初步学习了一下. 首先,注解跟注释不是一个意思,也根本不是同一个事物. 注释就是我们平常平常中对 ...