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 ...
随机推荐
- php新建数据库对象 基础知识
数据访问 结合html 数据库 PHP面向对象的方式函数的方式 1建立通道 MySQLi 类通过构造函数造出 连接数据库地址 uesername 连接用户名 passwd dbname port so ...
- TCP详解
1. 数据进入协议栈的封装过程 2. TCP连接的三次握手 3. TCP连接的三次握手和关闭时的四次握手 各个状态的意义如下: LISTEN - 侦听来自远方TCP端口的连接请求: SYN-SENT ...
- SuperWebClient -一个基于CURL的.NET HTTP/HTTPS模拟神组件(2)
今天我们讨论SuperWebClient组件使用中的几个简单主题 1: UserAgent2: Cookies3: POST登录 1:UserAgent这个是客户端标识信息,此信息是用于鉴别正在访问W ...
- 深入理解 JavaScript 异步系列(2)—— jquery的解决方案
第一部分,jQuery-1.5 之后的 ajax 本地址http://www.cnblogs.com/wangfupeng1988/p/6515779.html未经允许不得转载~ $.ajax这个函数 ...
- 徒手用Java来写个Web服务器和框架吧<第一章:NIO篇>
因为有个不会存在大量连接的小的Web服务器需求,不至于用上重量级服务器,于是自己动手写一个服务器. 同时也提供了一个简单的Web框架.能够简单的使用了. 大体的需求包括 能够处理HTTP协议. 能够提 ...
- Memcached【第二篇】高可用集群搭建
第一步:准备 1. 架构信息 利用 magent 搭建 memcached 集群,实现性能的高可用. IP Port 主从 192.168.6.129 11211 主节点 192.168.6.130 ...
- DOM基础(二)
在我之前写的DOM基础(一)的文章中提到过兼容性的问题,也就是在获取标签间文本信息的时候,早期的火狐版本是不支持innerText的,只支持textContent ,现在的火狐浏览器两者都支持.而IE ...
- java基础:数组查询,同一数组一个元素最多出现两次
- Bzoj超级经验大放送题集(好评如潮哦~~~)
其实这些是因为没有数据才形成的...唯一可惜的是这些都是需要300软妹币才能打开的萌萌哒权限题*^_^* 好啦,吾来教你如何快速AC么么哒 pascal: 1 begin end. //Pascal ...
- 2017年最新chrome必备插件推荐
1. 上网必备 Speed dial plus新标签页, 直接替换掉chrome自带的毫无新意的新标签页,简洁优美快速,我本人非常喜欢. &lt;img src="https ...