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. MyBatis从入门到精通(第9章):Spring集成MyBatis(中)

    MyBatis从入门到精通(第9章):Spring集成MyBatis(中) 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法.应该将应用自身的设计和具体 ...

  2. Vue专题-生命周期

    有时候,我们需要在实例创建过程中进行一些初始化的工作,以帮助我们完成项目中更复杂更丰富的需求开发,针对这样的需求,Vue提供给我们一系列的钩子函数. 本文详细介绍了Vue实例在创建和销毁的过程中,我们 ...

  3. 初次运行Git前的配置

    初次运行Git前的配置 一.初次运行 Git 前的配置 一般在新的系统上,我们都需要先配置下自己的 Git 工作环境.配置工作只需一次,以后升级时还会沿用现在的配置.当然,如果需要,你随时可以用相同的 ...

  4. DispatcherServlet和ContextLoaderListener,还有spring+servlet3.0 无web.xml启动问题

    上篇提到: 关于spring +springmvc中两个spring应用上下文(DispatcherServlet和ContextLoaderListener)的问题,挺让人迷糊的. 他们都是加载Be ...

  5. Kafka、RabbitMQ、RocketMQ等消息中间件的介绍和对比

    本博客强烈推荐: Java电子书高清PDF集合免费下载 https://www.cnblogs.com/yuxiang1/p/12099324.html 前言 在分布式系统中,我们广泛运用消息中间件进 ...

  6. 简述cookies 和 session

    session HTTP协议本身是无状态的,本身并不能支持服务端保存客户端的状态信息,于是,Web Server中引入了session的概念,用来保存客户端的状态信息. 1)当一个用户向服务器发送第一 ...

  7. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)报错

    0 环境 系统环境:win10 1 正文 先检查Mapper接口与相关联xml文件是否对应,需要检查包名,namespace位置是否写对,curd时id名称等能否对应上 常规步骤: :检查mapper ...

  8. easyui分页控件的应用

    1.首先应用easyui的js和css文件 <link rel="stylesheet" type="text/css" href="../.. ...

  9. 结构体初始化和new delete

    int *p; p=new int[100]; delete []p; 结构体中的指针需要初始化

  10. 正则表达式中 /s 可以帮助“.”匹配所有的字符,包括换行,从而实现【\d\D】的功能

    正则表达式中 /s 可以帮助“.”匹配所有的字符,包括换行,从而实现[\d\D]的功能