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"].
这道题的思路很简单,假设输入的是"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(回溯)的更多相关文章
- [LeetCode] Letter Combinations of a Phone Number 回溯
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [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][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 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 ...
随机推荐
- (一〇九)UIButton的使用技巧 -imageView、titleLabel、圆角等
UIButton是一个常用控件,使用方法十分基本,但是有很多技巧常常不被注意,本文主要介绍UIButton的一些较高级技巧,用于实现图片和标签显示的美观性等. 开发时常常碰到按钮的下侧或者右侧有标题的 ...
- 极光推送---安卓Demo
对于一个一直干.net的程序媛来说,冷不丁的让小编干安卓,那种感觉就好似小狗狗咬小刺猬一样,不知道从哪儿开始下手,对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下 ...
- input事件--->按键事件的基本实现
本程序基于TINY4412开发板,程序已经验证过,完全正确: 那么,如何来写这样的一个驱动程序呢? 1.分配一个input_dev结构体 2.设置 3.注册 4.硬件相关的代码,比如中断,定时器,休眠 ...
- Java-IO之管道(PipedInputStream和PipedOutputStream)
java中PipedInputStream和PipedOutputStream分别是管道输入流和管道输出流,它的作用是让多线程可以通过管道进行线程间的通讯,在使用管道通信时,必须将PipedInput ...
- XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象
Demo1类: import java.io.File; import java.util.List; import javax.xml.parsers.SAXParser; import javax ...
- Cocos2D:塔防游戏制作之旅(九)
炮塔哲学:敌人,攻击波和路径点 在创建敌人之前,让我们先为它们"铺路".敌人将沿着一系列的路径点前进,这些路径点互相连接,它们被定义为敌人在你创建的世界中移动的路径. 敌人将在第一 ...
- 04-GIT TortoiseGit冲突和补丁演示 案例演示
TortoiseGit安装下载 http://download.tortoisegit.org/tgit/1.8.12.0/ 或https://code.google.com/p/tortoisegi ...
- Chapter 2 User Authentication, Authorization, and Security(11):在已还原的数据库中修正登录映射错误
原文出处:http://blog.csdn.net/dba_huangzj/article/details/39496517,专题目录:http://blog.csdn.net/dba_huangzj ...
- VC++读取图像RGB值
代码: #include <iostream> #include <fstream> #include <string> #include <windows. ...
- Socket编程实践(7) --Socket-Class封装(改进版v2)
本篇博客定义一套用于TCP通信比较实用/好用Socket类库(运用C++封装的思想,将socket API尽量封装的好用与实用), 从开发出Socket库的第一个版本以来, 作者不知道做了多少改进, ...