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. ArrayList集合存储VO封装对象后调用的问题

    VO代码: public class VO4Blog { private int b_id; private int b_typeid; private String b_title; private ...

  2. Java多线程求和

    package test; import java.util.concurrent.*; import java.util.concurrent.locks.Lock; import java.uti ...

  3. Python dict 和 list 转换

    这里有个dict d1 = { 'en':'英语', 'cn':'中文', 'fr':'法语', 'jp':'日语' } 使用d1.keys()或 d1.values() 可以提取出values 和k ...

  4. 面向对象 part3 构造函数 原型函数

    6.2创建对象 方法:对象字面量  object构造函数 缺点:都是创建单个对象.同一个接口创建多个对象,会产生大量重复代码 6.2.1工厂模式 用函数封装以特定的接口创建对象 function cr ...

  5. win32框架

    win32的框架 1.入口函数 2.窗口注册类信息 3.窗口创建 4.显示窗口 5.更新窗口 6.消息循环 7.入口函数结束 WNDCLASSEX wcex;窗口类结构 wcex.cbSize = s ...

  6. 自由职业平台Upwork申请上市,能给国内猪八戒网带来什么启示?

    当下,在互联网的全面渗入下已经对整个社会的运行机制.大众的生活产生了前所未有的影响.尤其是在工作方面,更是衍生出诸多新兴职业.如,主播.水军.新媒体运营.自媒体人等.值得注意的是,这些职业绝大部分都有 ...

  7. 1.docker 的 安装

    1.mac 安装docker 1.1 打开 https://docs.docker.com/docker-for-mac/install/ 1.2 下载 dmg 文件 1.3  打开 docker,d ...

  8. MobileNets: Open-Source Models for Efficient On-Device Vision

    https://research.googleblog.com/2017/06/mobilenets-open-source-models-for.html  Wednesday, June 14, ...

  9. 为什么使用 document.write 需要将</script>拆分开

    福州SEO:细心点的朋友可能会注意到,有些网站使用document.write动态加载JS的时候需要把</script>拆分开来写?如下面的例子所示: <script type='t ...

  10. mysql命令行操作大全

    Mysql安装目录 数据库目录/var/lib/mysql/配置文件/usr/share/mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysql ...