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. PythonStudy——字符串常用操作 String common operations

    # 1.字符串的索引取值: 字符串[index]# 正向取值从0编号,反向取值从-1编号 s1 = '123abc呵呵' t_s = ' # 取出c print(s1[5], s1[-3]) # 2. ...

  2. uml类图符号

    符号及实例参照:http://www.blogjava.net/cnfree/archive/2012/10/30/390457.html https://blog.csdn.net/l_nan/ar ...

  3. oracle linux 6.8 安装

    ' 测试环境vm虚拟机 硬盘大小50G 内存2G CPU 4 选择install or upgrade an existing system 选择skip跳过内存检查 Next 选择语言,Next 选 ...

  4. phpize是什么

    安装php(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize一.phpize是干嘛的?phpize是什么东西呢?php官方的说明: ...

  5. *浅解嵌入式中的BootLoader

    本文只作为本人学习过程中的记录及时不时的突发奇想偶记.鄙人菜鸟一只,文中如有错误或疏漏,若读者肯不吝赐教,在下感激零涕.文章一直不断更新中 一.何为Bootloader 在嵌入式系统中,Bootloa ...

  6. 学习笔记之Naive Bayes Classifier

    Naive Bayes classifier - Wikipedia https://en.wikipedia.org/wiki/Naive_Bayes_classifier In machine l ...

  7. Js将数字转化为中文大写

    function number_chinese(str) { var num = parseFloat(str); var strOutput = "", strUnit = '仟 ...

  8. Github 大牛封装 Python 代码,实现自动发送邮件只需三行代码

    *注意:全文代码可左右滑动观看 在运维开发中,使用 Python 发送邮件是一个非常常见的应用场景.今天一起来探讨一下,GitHub 的大牛门是如何使用 Python 封装发送邮件代码的. 一般发邮件 ...

  9. 面试回顾——kafka

    关于消息队列的使用场景:https://www.cnblogs.com/linjiqin/p/5720865.html kafka: Topic Kafka将消息种子(Feed)分门别类 每一类的消息 ...

  10. SpringBoot 之静态资源

    boot 的默认的静态资源有多个, 由 ResourceProperties 配置了默认值: private static final String[] CLASSPATH_RESOURCE_LOCA ...