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.

Time: O(3^N)

class Solution {
private String[] letters = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return res;
}
helper(res, "", digits, 0);
return res;
} private void helper(List<String> res, String s, String digits, int index) {
if (s.length() == digits.length()) {
res.add(s);
return;
}
String curLetter = letters[digits.charAt(index) - '0'];
// loop through current letters[index]
for (int i = 0; i < curLetter.length(); i++) {
helper(res, s + curLetter.charAt(i), digits, index + 1);
}
}
}

[LC] 17. Letter Combinations of a Phone Number的更多相关文章

  1. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  2. 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 ...

  3. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

  4. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

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

  6. 17. Letter Combinations of a Phone Number

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

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

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

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

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

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

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

随机推荐

  1. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

  2. Python笔记_第四篇_高阶编程_实例化方法、静态方法、类方法和属性方法概念的解析。

    1.先叙述静态方法: 我们知道Python调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...

  3. 201512-1 数位之和 Java

    思路: mod 10获取最低位,除以10去掉最低位 import java.util.Scanner; public class Main { public static void main(Stri ...

  4. NFS 文件共享

    备注:NFS 文件共享需设置两部分:服务端和客户端 一.服务端设置 1.1.查看nfs包是否安装,未安装则重新安装 [root@localhost ~]# rpm -qa|grep rpcbind r ...

  5. 应用gis笔记

    接口,开发包??我要做一个移动端的,完了之后和5G挂一下钩, web/桌面/移动 C#就是.NET.... 和专业程序设计课程的区别 a kind of boring hope it helpful, ...

  6. gcc -E xx.c

    C语言代码在交给编译器之前,会先由预处理器进行一些文本替换方面的操作,例如宏展开.文件包含.删除部分代码等. 在正常的情况下,GCC 不会保留预处理阶段的输出文件,也即.i文件.然而,可以利用-E选项 ...

  7. Codeforces Round #600 (Div. 2)E F

    题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...

  8. jetbrains系列产品 永久注册方法

    使用方法: 0. 先下载压缩包解压后得到jetbrains-agent.jar,把它放到你认为合适的文件夹内. 下载列表地址:https://zhile.io/files/jetbrains-agen ...

  9. 联想的amd电脑,Debian8.8开机后亮度值始终最大,尝试过各种方法,始终无法解决,最后debian8.8在安装开源驱动后,成功调节

    安装ATI显卡驱动(开源)(方法步骤来自Debian WiKi) A.先升级可用的包 # aptitude upgrade B.安装下面3个包 # apt-get install firmware-l ...

  10. Linux介绍,基本命令