Letter Combinations of a Phone Number:深度优先和广度优先两种解法
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:深度优先和广度优先两种解法的更多相关文章
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
随机推荐
- 浅谈sql优化
问题的发现: 菜鸟D在工作的时候发现项目的sql语句很怪,例如 : select a.L_ZTBH, a.D_RQ, a.VC_BKDM, (select t.vc_name from tb ...
- 使用jQuery快速高效制作网页交互特效
第四章:JQuery选择器 1.Jquery选择器简介 (1) Jquery中的选择器完全继承了CSS的风格,利用Jquery选择器,可以非常便捷和快速的找出特定的Dom元素,然后为他们添加相应的行为 ...
- configparser配置文件模块
1.configparser的作用 mysql等很多文件的配置如下: [DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLeve ...
- window编程之win程序框架
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmd ...
- angular : ng-animate : css 原理,详解
通过几中指令就能完成1.2.xx的animate ·ng-repeat ·ng-show,ng-hide ·ng-if,ng-include,ng-view ·ng-switch ·ng-class ...
- Form表单中method="post/get'的区别
Form提供了两种数据传输的方式--get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...
- javascript深入理解js闭包(转载)
一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...
- 第21篇 js四种继承方式
js是一个很自由的语言,没有强类型的语言的那种限制,实现一个功能往往有很多做法.继承就是其中的一个,在js中继承大概可以分为四大类,上面一篇文章也提及过一些,下面开始详细说说js的继承. 1.原型继承 ...
- 【曝】苹果应用商店逾千款iOS应用存安全漏洞
据国外网站Ibtimes报道,知名网络安全公司FireEye日前警告称,由于一款名为“JSPatch”.可帮助开发者修改应用程序的软件上存在安全漏洞,导致苹果应用商店内1000多款使用了该框架的iOS ...
- java 线程中断机制
上一篇文章我们了解过了java有关线程的基本概念,有线程的属性,线程可能处于的状态,还有线程的两种创建的方式,最后还说了一个关键字synchronized,解决了高并发导致数据内容不一致问题,本篇文章 ...