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. ubuntu连接android设备(附最简单方法)

    在ubuntu下连接android设备,虽然不用像windows那样安装驱动,然而却会遇见一个错误:输入adb shell,会提示insufficient permissions for device ...

  2. UI设计--大象无形

      UI设计,大象无形 UI设计,如同优雅的艺术品一样,优秀的UI设计也可以大象无形,大象无形的意思是有意化无意.大象化无形!就是不要显刻意,不要过分的主张,要兼容百态.无形态无框架才能容纳一切形体! ...

  3. 饮一盏Bug留香,唱一曲项目飞扬

    沿途的风景    牵挂的项目    两情迢迢 学生档案管理项目在2月的末尾从稍带寒意的季节里完成了第一次迭代,验收的结果不尽善尽美,演示的功能也惨不忍睹,各种"关爱"的点评充斥耳旁 ...

  4. Android的SeekBar和RateBar的使用-android学习之旅(三十二)

    SeekBar简介 SeekBar允许用户拖动,进行调节经常用于音量调节等方面. android:thumb设置drawable对象来表示拖动的物体. setOnSeekBarChangeListen ...

  5. matlab函数interp2及其c++代码

    最近将一个matlab程序转为c++,途中遇到interp2这个家伙,我是左查右查,发现网上没有人总结这个玩意,于是我来初探一下,还是别有洞天的,嘿嘿. 1.关于interp2 Vq = interp ...

  6. mysql DISTINCT 的实现与优化

    DISTINCT实际上和GROUP BY的操作非常相似,只不过是在GROUP BY之后的每组中只取出一条记录而已.所以,DISTINCT的实现和GROUP BY的实现也基本差不多,没有太大的区别.同样 ...

  7. 连接器与容器的桥梁——CoyoteAdapter

    如果把整个tomcat内核最高抽象程度模块化,可以看成是由连接器Connector和容器Container组成,连接器负责HTTP请求接收及响应,生成请求对象及响应对象并交由容器处理,而容器则根据请求 ...

  8. Oracle Instance

    以前也学习过oracle 逻辑结构的知识,但用的不多好多都是有点概念,最近做到一个跨instance工作流,所有抽点时间温习了一下相关知识,把网上看到的感觉讲的还比较明了,全面的文章汇总一下 inst ...

  9. (五十五)iOS多线程之GCD

    GCD的全称为Grand Central Dispatch,翻译为大中央调度,是Apple开发的一个多线程编程解决方法. 进程和线程的概念: 正在进行中的程序被称为进程,负责程序运行的内存分配,每一个 ...

  10. numpy教程:逻辑函数Logic functions

    http://blog.csdn.net/pipisorry/article/details/48208433 真值测试Truth value testing all(a[, axis, out, k ...