Given a digit string, 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.

Input:Digit string "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.

解题思路:

需要穷举不同中排列可能,典型需要回溯的思想;

可以选择递归或非递归的形式实现回溯;

递归:

 class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> lst;
string ans;
if (digits == "")
return lst;
Backtracking(lst, ans, digits, );
return lst;
} void Backtracking(vector<string> &lst, string ans, string digits, int idx) {
if (idx == digits.size()) {
lst.push_back(ans);
return;
}
string cur_chars = d2l[digits[idx] - ''];
for (int i = ; i < cur_chars.size(); ++i)
Backtracking(lst, ans + cur_chars[i], digits, idx + );
} private:
vector<string> d2l = {
" ", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"
};
};

非递归:

【Leetcode】【Medium】Letter Combinations of a Phone Number的更多相关文章

  1. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

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

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

  7. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  8. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

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

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

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

随机推荐

  1. C# 特性(Attribute)之Flag特性

    本文参考自C# 位域[flags],纯属读书笔记,加深记忆 [Flags]的微软解释是“指示可以将枚举作为位域(即一组标志)处理.”其实就是在编写枚举类型时,上面附上Flags特性后,用该枚举变量是既 ...

  2. 常用linux网络工具

    iftop netstat nethogs可以查看进程占用网络的情况 nc -u -z -w2 192.168.0.1 1-1000 //扫描192.168.0.3 的端口 范围是 1-1000

  3. 关于定位和z-index的一些小经历

    今天在工作过程中,遇到这么一个奇葩问题,搞了好一阵子才找到原因,遂总结了一下... 先上DEMO: <div style="width:800px; height:400px;&quo ...

  4. Python基础(8) - 模块

    Python 模块的物理形式就是文件:一个文件对应一个模块.文件名就是模块名+.py 模块定义了自己独有的命名空间.在其定义的属性,函数,类都隶属于该空间. 通过import关键字我们可以导入模块: ...

  5. 跨文件代码跳转插件:Ctags

    1.通过package control搜索Ctags 2.Enter安装,等待其安装完成 3.下载ctags可执行程序,链接:https://pan.baidu.com/s/1jIINAxo 密码:4 ...

  6. 如何通过 PHP 获取 Azure Active Directory 令牌

    在调用 Azure Rest API 时,如果是属于 Azure Resource Manager 的 API,则需要使用 Azure Active Directory (Azure AD)认证获取令 ...

  7. JMS - 基于JMS的RPC

    现在试试通过JMS,在应用程序之间发送消息.先看看spring提供的RPC方案(其实还有其他方案,只是没见过谁用).需要使用到这两个类:·org.springframework.jms.remotin ...

  8. Backbone之温故而知新1-MVC

    在忙碌了一段时间之后,又有了空余时间来学习新的东西,自从上次研究了backbone之后,一直不得入门,今天有时间有温故了一次,有了些许进步在此记录下, 在开始之前,不得不提一下我的朋友给了我“豆瓣音乐 ...

  9. JS实现单链表、单循环链表

    链表 链表是一种物理存储单元上非线性.非连续性的数据结构(它在数据逻辑上是线性的),它的每个节点由两个域组成:数据域和指针域.数据域中存储实际数据,指针域则存储着指针信息,指向链表中的下一个元素或者上 ...

  10. node版本的切换(转)

    大量开发者的贡献使Node版本的迭代速度很快,版本很多(横跨0.6到0.11),所以升级Node版本就成为了一个问题.目前有n和nvm这两个工具可以对Node进行无痛升级,本文简单介绍一下二者的使用. ...