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 ...
随机推荐
- ASP.NET通用权限框架 权限管理系统源码jquery 精美UI源码
软件技术开发,合作请联系QQ:858-048-581 开发工具 VS2010 .sql2005.2008等(在Sql server数据执行脚本即可) VS2010 打开保证本地运行成功(数据库.源代 ...
- jQuery给CheckBox全选与不全选
$(function(){ $("#checkAll").click(function() {//全选 $('input[name="DATA"]').prop ...
- java中map集合的迭代
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TestMap { pu ...
- SQLServer索引循环删除
declare qc_cursor cursor SCROLL OPTIMISTIC Forselect siteName from tb_vhostcheckopen qc_cursordeclar ...
- redux-form的学习笔记
redux是一种常用的与react框架搭配的一种数据流架构,而伴随着redux的出现,也出现了许多基于redux开源的第三方库,而redux-form就是其中之一的开源组件库,到今天我写下这篇笔记为止 ...
- 使用IDEA的gradle整合spring+springmvc+mybatis 采用javaconfig配置
1.在上篇博客里讲述了spring+mybatis的整合,这边在上篇的基础上进行开发. 上篇博客链接http://www.cnblogs.com/huangyichun/p/6149946.html ...
- adt的问题An internal error has occurred. After scene creation, #init() must be called
这个问题困扰了我好久,我也尝试去百度.google无济于事啊,让我寝食难安,太难受了,我把它贴出来,希望后人不绕弯子... 解决办法: 即可,解决这一个问题,现在酣畅淋漓,挥洒自如的capy代码了
- laravel 简单的上传图片
/** * laravel 简单的上传图片* @param Request $request* @return View*/public function upload(Request $reque ...
- 简单的add函数的N种写法
最近在学习es6,看到for-of这里,就想自己写着练习一下,于是就准备写一个小函数add来求和.函数很简单,如add(1,2,3)这样.于是我开始着手 一开始我是这么写的 function add( ...
- Ioc在重构代码中的应用
最近lz在写抓工商公式系统(http://www.gsxt.gov.cn/index.html)的爬虫,其中的难点就是在怎么过极验验证码,搞的我不要不要的!如下: 简直是各种坑,被搞的死去活来以后还是 ...