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 ...
随机推荐
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
废了半天劲才解决... 就三步:菜单栏,Tools -> Adnroid -> enable ADB integration
- 07_NoSQL数据库之Redis数据库:Redis的高级应用之事务处理、持久化操作、pub_sub、虚拟内存
事务处理 Redis对事务的支持目前还比较简单.Redis只能保证一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令.当一个client在一个连接中发出mul ...
- 初识WCF之使用配置文件部署WCF应用程序
二月份的开头,小编依旧继续着项目开发之路,开始接触全新的知识,EF,WCF,MVC等,今天小编来简单的总结一下有关于WCF的基础知识,学习之前,小编自己给自己提了两个问题,WCF是什么?WCF能用来做 ...
- Docker教程:使用docker配置python开发环境
http://blog.csdn.net/pipisorry/article/details/50808034 Docker的安装和配置 [Docker教程:docker的安装] [Docker教程: ...
- Socket编程实践(10) --select的限制与poll的使用
select的限制 用select实现的并发服务器,能达到的并发数一般受两方面限制: 1)一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n(number)来调整或 ...
- 《java入门第一季》之tcp协议下的网络编程c/s实现通信交互
需求:客户端向服务器发送数据,服务器端收到数据后向客户端返回数据: 还是使用两台电脑,一台客户端,一台服务器. 客户端代码: import java.io.IOException; import ja ...
- 海量数据挖掘MMDS week5: 聚类clustering
http://blog.csdn.net/pipisorry/article/details/49427989 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- 多进程log4cxx区分日志
多进程log4cxx区分日志 (金庆的专栏) 网游客户端一般会多开,多个进程会写同一个日志文件.log4cxx看来会对文件加锁,防止多进程写同一文件写乱,截止目前还没发现错乱的日志. log4cxx有 ...
- Socket层实现系列 — 睡眠驱动的同步等待
主要内容:Socket的同步等待机制,connect和accept等待的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 概述 socket上定义了 ...
- Leetcode_112_Path Sum
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41910495 Given a binary tree an ...