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"].

这道题的思路很简单,假设输入的是"23",2对应的是"abc",3对应的是"edf",那么我们在递归时,先确定2对应的其中一个字母(假设是a),然后进入下一层,穷举3对应的所有字母,并组合起来("ae","ad","af"),当"edf"穷举完后,返回上一层,更新字母b,再重新进入下一层。这个就是backtracing的基本思想。

遍历当前数字对应的字符,对于每个字符,都要添加到字符串中,然后遍历剩下的数字,并添加剩下数字的字符(这个继续递归,控制index),所以有个指针指向当前数字,这就是index

class Solution {
/*
这道题的思路很简单,假设输入的是"23",2对应的是"abc",3对应的是"edf",那么我们在递归时,先确定2对应的其中一个字母(假设是a),然后进入下一层,穷举3对应的所有字母,并组合起来("ae","ad","af"),当"edf"穷举完后,返回上一层,更新字母b,再重新进入下一层。这个就是backtracing的基本思想。
*/
String[] map=new String[]{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public List<String> letterCombinations(String digits) { List<String> list=new ArrayList<>();
if(digits==null||digits.length()==0) return list;
helper(list,"",digits,map,0);
return list; } /*
各个参数意思:current 表示当前字符串(用于添加到list中的,结果串),index:digits中的第几位,用于判断
*/
public void helper(List<String> list,String current,String digits,String[] map,int index){
//最后一层的退出条件,index从0开始的,当等于digits的长度时,也就是所有数字都遍历过了,可以结束了
if(index==digits.length()){
if(current.length()!=0) list.add(current);
return ;
} //index不是最后一层,那就遍历这个数字对应的字母,并添加剩下数字对应的字母
String s=map[digits.charAt(index)-'0'];
//遍历当前数字对应的字符,并添加剩下数字的字符(这个继续递归,控制index)
for(int i=0;i<s.length();i++){
String next=current+s.charAt(i);//这里用next,而不是直接用current ,因为这里current不能被修改,因为遍历时每个字母都是单独加在current上
//进入下一层
helper(list,next,digits,map,index+1);
}
}
}

letter combinations of a phone number(回溯)的更多相关文章

  1. [LeetCode] Letter Combinations of a Phone Number 回溯

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

  2. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  3. 【leetcode】Letter Combinations of a Phone Number

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

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

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

  5. 69. Letter Combinations of a Phone Number

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

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

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

  7. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

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

  8. 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 ...

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

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

随机推荐

  1. android 填满手机磁盘空间方法

    http://blog.csdn.net/fulinwsuafcie/article/details/9700619 很多时候我们需要进行临界测试. 譬如当手机盘空间存满的条件下应用会有何表现等. 之 ...

  2. 文章标题 Oracle数据库中dual表使用

    一. 业务场景 业务流程需要进行写入和更新的比较,所以有原表和历史表. 要求表中的ID唯一性,以及两张表的ID关联,另外后续可能数据库会进行迁移 二.方案选择 方案一:id设置为int型自增长. 这种 ...

  3. ubuntu14.04使用root用户登录桌面

    ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...

  4. python跨行 print:多用(),换行符\要小心,少用+或者不用(其它程序代码跨行用\就行,不能用括号)

    这两种是错的 # print '11' #       'tset3'#error # print '12' #       +'tset4'#error python跨行用()和\都能实现.+只是连 ...

  5. javascript setinterval 正确的语法

    前几天我用setinterval 写了一个小程序,这个setinterval是用来干什么的我就不解释了. 写的方法在其它的浏览器里都能用,后来测试组的同事拿去一测就出了问题.因为她们爱用360,还有I ...

  6. JAVA DOM4j解析XML数据到自定义javabean

    我们获取xml中的数据,一般以面向对象的思想去处理这些数据.因此,我们需要自定义类来封装解析出来的数据,以方便我们操作这些数据. 自定义的java类,称为javabean. 自定义Contact类代码 ...

  7. Android反编译获取源码-android学习之旅(70)

    上一讲我们介绍了如何获取资源文件,这一节讲解如何获取源码,其实获取源码真的很简单 首先还是要有工具,Dex2jar,这个工具用于将apk解压之后的dex文件转化为jar文件还有jd-gui的这个工具能 ...

  8. Android 仿百合网超火爆社交app首页滑动效果

    探探,百合网等神器的首页有一个相册加载个控件,通过左滑右滑加载新的照片,同时左滑丢弃这个照片,右滑则表明对这个照片感兴趣. 这个效果是怎么实现的呢? 1,Android3.0以后控件中增加了setTr ...

  9. android 签名

    (1)Android Studio菜单Build->Generate Signed APK (2)弹出窗口 (3)创建密钥库及密钥,创建后会自动选择刚创建的密钥库和密钥(已拥有密钥库跳过)    ...

  10. Java进阶(十一)部分数据类型取值范围

    java整型byte,short,int,long取值范围大小 在项目开发中,需要用到随机数的生成方法,代码如下: public static String randomizeNumber(int n ...