Leetcode17--->Letter Combinations of a Phone Number(电话号码对应的字母的组合)
题目:
给定一个数字字符串,返回数字所能代表的所有字母组合;
举例:
![]()
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
解题思路:
1. 首先要判断给定的数字字符串是否合法(isDigit()方法),即是否只包含0~9的数字,如果还包含其他,则直接返回空;
2. 其次要将每个电话号码所对应的字符串保存起来,这里我使用的是HashMap,key是数字,value是数字所对应的字符串;
3. 采用递归的方式将数字所代表的字母连接起来;举例:234,则对应的是abc,def,ghi,此时首先选择a,然后选择d,然后选择g,得到一个结果adg,选择h,得到一个结果adh,选择i,得到结果adi;选择e.选择g得到结果aeg,以此类推......
代码如下:
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<String>();
if(digits == null || digits.length() < 1){
return res;
}
if(!isDigit(digits)) return res;
HashMap<Character, String> hm = new HashMap<Character, String>();
hm.put('0', "");
hm.put('1', "");
hm.put('7', "pqrs");
hm.put('8', "tuv");
hm.put('9', "wxyz");
String str = "abcdefghijklmno";
int i = 2;;
int j = 0;
while(i < 7){
hm.put((char)(i + '0'), str.substring(j, j + 3));
j = j + 3;
i++;
}
char[] ch = new char[digits.length()];
isCal(digits, 0, ch, res, hm);
return res;
}
// 判断给定的数字字符串是否合法,不合法,统一返回空
public boolean isDigit(String digits){
for(int i = 0; i < digits.length(); i++){
if(digits.charAt(i) < '0' || digits.charAt(i) >'9')
return false;
}
return true;
}
// index代表的是第几个数字
public void isCal(String digits, int index, char[] ch, List<String> res, HashMap<Character, String> hm){
if(index == digits.length()){ // 如果成立,表明已经连接到最后一个数字了,因此要将结果加入res
res.add(new String(ch));
return;
}
char c = digits.charAt(index);
for(int i = 0; i < hm.get(c).length(); i++){ //
ch[index] = hm.get(c).charAt(i); // 获取index所对应数字的字符串中的字符
isCal(digits, index + 1, ch, res, hm); // 获取下一个数字所对应的字符串中的字符
}
}
}
Leetcode17--->Letter Combinations of a Phone Number(电话号码对应的字母的组合)的更多相关文章
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- [LintCode] 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
Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...
- [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 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- LeetCode17 Letter Combinations of a Phone Number
题意: Given a digit string, return all possible letter combinations that the number could represent. A ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
随机推荐
- Redhat/CentOS 软件安装
概述 软件包简介 rpm 包安装 yum 在线安装 源码包管理 软件包分类 源码包 二进制包(rpm包.系统默认包) rpm包安装 rpm包的依赖性 树形依赖: a -> b -> c 环 ...
- Appium基础一:Appium概念
1.Appium介绍: Appium是一款开源跨平台(IOS和Android平台)支持多种开发语言(java.python等)进行测试Native/Web/Hybrid的Android/iOS App ...
- SQL Server 2012安装配置(Part3 )
SQL Server 2012安装配置(Part1 ) SQL Server 2012安装配置(Part2 ) SQL Server 2012安装配置(Part3 ) 3 客户端安装 3.1 安装客户 ...
- python3操作excel02(对excel的基础操作,进行简单的封装)3
#!/usr/bin/env python# -*- coding:UTF-8 -*- import requestsfrom bs4 import BeautifulSoupfrom bs4 imp ...
- 使用ABAP批量下载有道云笔记中的图片
Jerry喜欢用有道云笔记这款软件做自己的知识管理和知识体系的构建. 当您看到一篇好的有道云笔记分享时,可能会想将其精美的图片下载到本地.作为程序猿,我们不会去手动一张张下载.写个程序帮我们自动下载吧 ...
- 删除表中一个字段的SQL语句
1.删除没有默认值的列:alter table Test drop COLUMN BazaarType 2.删除有默认值的列:先删除约束(默认值)alter table Test DROP CONST ...
- ansible 通过堡垒机/跳板机 访问目标机器需求实战(ssh agent forward)
一. 需求背景: 在我们使用ansible的过程中经常会遇到这样的情况,我们要管理的机器都在内网中,这些内网机器的登录都是通过跳板机或者堡垒机登录.我们的ansible机器不能直接管理到这些后端的机器 ...
- Java写诗程序
import java.util.Random; public class test_word { public static void main(String[] args) { System.ou ...
- 记录一次mysql中自定义获取UUID的函数
循环方式一: DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() ) begin ) ...
- Java中的Static修饰符
static(静态.修饰符):static修饰成员变量时:static修饰成员变量时,那么该成员变量的数据就是一个共享的数据. 静态成员变量的访问方式:方式一: 使用对象进行访问. 对象.属性名 方式 ...