17. Letter Combinations of a Phone Number[M]电话号码的字母组合
题目
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.

思路
思路一:递归法
首先建立电话按钮上数字与字母的对应关系,很容易就想到了利用C++里map
思路二:回溯法
Tips
递归法
回溯法
C++
- 思路一
map<int,string> table={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};
vector<string> letterCombinations(string digits) {
vector<string> result;
if(digits.length()==0)
return result;
combineNum(result,0,"",digits);
return result;
}
void combineNum(vector<string>& r,int count,const string& a,const string& b){
if (count == b.size()){
r.push_back(a);
return;
}
string curStr = table[b[count] - '0'];
for (char s : curStr){
combineNum(r, count + 1, a + s, b);
}
}
- 思路二
python
17. Letter Combinations of a Phone Number[M]电话号码的字母组合的更多相关文章
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- html中map标签和area标签的应用
map标签的用途:是与img标签绑定使用的,常被用来赋予给客户端图像某处区域特殊的含义,点击该区域可跳转到新的文档. 因为map标签是与img标签绑定使用的,所以我们需要给map标签添加ID和name ...
- Splay树
class SplayNode { public: SplayNode *child[]; char value; int size; bool flip; SplayNode(), flip(fal ...
- 以shareExtension为例学习iOS扩展开发
整体介绍 phone Extension 用法基础详解 share Extension 用法基础详解 demo链接 密码: i72z
- [转]Git入门与实践(一)
git入门与实践(一) · March 10th, 2010 · Posted in UNIX环境编程 · By ghosTM55 Write comment ...
- Vs2010无法打开文件“Kernel32.lib”、无法打开“libcpmt.lib”"msvcprt.lib"
1.对于无法打开"Kernel"问题,即使复制lib文件到目录,仍然会出现最后的错误; 原因:WindowsSdk 安装失败! 方法:重装 microsoft SDK6.0 ,再在 ...
- Laravel5.6安裝:Warning: require(../vendor/autoload.php): failed to open stream: No such file or directory
在phpstudy下使用composer+laravel安装器的方式安装了Laravel,但是访问的时候报错: Warning: require(D:\phpstudy\WWW\public\mybl ...
- PHP中each与list用法分析
1.each的用法 先看API array each ( array &$array ) api里是这么描述的:each — 返回数组中当前的键/值对并将数组指针向前移动一步 我们先来看看返回 ...
- 洛谷P3807 【模板】卢卡斯定理_组合数学模板
Code: #include<cstdio> using namespace std; typedef long long LL; const int maxn=1000000+2; LL ...
- js和css引入文件消除缓存
?version=<?php echo filemtime('引用路径'); ?>
- 原生node实现简易留言板
原生node实现简易留言板 学习node,实现一个简单的留言板小demo 1. 使用模块 http模块 创建服务 fs模块 操作读取文件 url模块 便于path操作并读取表单提交数据 art-tem ...