Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

题意:

给定一个数字串,看看有多少种对应的字母串。

思路:

Assumption:

字符串的合法性,是否包含1这个数字?如果包含,怎么处理?同样,输入是否考虑 * 或 #?

空字符串怎么处理?

多个解按什么顺序返回?

High Level带着面试官walk through:

take "23" for example:

when index = 0,  pointing to '2' in "23"

String letters  =  "abc"

for each char in letters,

add to path

move index forward, pointing to '3' in "23",  recursively call the helper function to add every char of "def" to the path

code

 class Solution {
private static String[] keyboard =
new String[]{ " ", "", "abc", "def", // '0','1','2',...'9'
"ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; public List<String> letterCombinations(String digits) { //"23"
List<String> result = new ArrayList<>();
if(digits.length() == 0 ) return result;
helper(digits, 0, new StringBuilder(), result);
return result;
} private void helper(String digits, int index, StringBuilder sb, List<String> result){
if(index == digits.length()){
result.add(sb.toString());
return;
}
String letters = keyboard[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++ ){
char c = letters.charAt(i);
sb.append(c);
helper(digits, index+1, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
}
}

注意: 这个题也可以用String path来存每条路径。 可是我自己觉得用StringBuilder这么动态伸缩,更显得有“Backtracking”的味道。

[leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合的更多相关文章

  1. Leetcode 17. Letter Combinations of a Phone Number(水)

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

  2. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  3. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  5. Leetcode 17.——Letter Combinations of a Phone Number

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

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

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

  7. [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  8. 17. Letter Combinations of a Phone Number[M]电话号码的字母组合

    题目 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...

  9. LeetCode——17. Letter Combinations of a Phone Number

    一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...

随机推荐

  1. how to tell gcc with c99 enable

    just copy the make file here. CC = gccCFLAGS = -Wall -std=c99OUTFILE = outputfileOBJS = source.oSRCS ...

  2. 100道JS构造函数面试题

    1. var User = { count: 1, getCount: function () { return this.count; } }; console.log(User.getCount( ...

  3. 策略模式(Strategy )

    为实现一个目的采用不同的方式都可实现,具体看要采取哪种方式. //接口 public interface Strategy {    public void algorithmInterface(); ...

  4. Docker之数据卷Volume(七)

    一.简介   Docker数据卷(volume)机制.volume是存在于一个或多个容器中的特定文件或文件夹,这个目录以独立于联合文件系统的形式在宿主机中存在,并为数据的共享与持久化提供便利. 1)v ...

  5. 【转】C++中嵌入python程序——参数传递

    C++中嵌入python程序——参数传递 前面两篇博客已经介绍如何在C++中嵌套使用 python,但是在实际使用中,我们需要向python传递各种各样的参数,这样的程序才具有更高的灵活性.下面简单介 ...

  6. c#读sql server数据添加到MySQL数据库

    using System;using System.Collections.Generic;using System.Text;using Console = System.Console;using ...

  7. MDX函数

    MDX重点函数 成员函数 1..CurrentMember 获取运行时当前的成员,用法:<Dimension>.CurrentMember . 2..Parent 获取运行时当前的成员的父 ...

  8. Linux centos7. 配置安装Oracle

    oralcle 11g r2 配置一下前期的网络环境 一 修改linux核心配置 1.修改用户的SHELL限制vi /etc/security/limits.conf oracle soft npro ...

  9. 小程序https请求,http网站升到https

    最近开发小程序,因为以前只写过小程序的前端没注意接口,现在才发现原来所有的接口都必须使用https协议了,马上研究了一波,顺便也想给自己的博客升成https的. 申请免费证书 哈哈没办法就是喜欢免费的 ...

  10. 查找二叉树(tree_a)

    问题 E: 查找二叉树(tree_a) 时间限制: 1 Sec  内存限制: 128 MB提交: 206  解决: 152[提交][状态][讨论版][命题人:quanxing][Edit] [Test ...