【leetcode】 Letter Combinations of a Phone Number(middle)
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> lux();
lux[] = "abc"; lux[] = "def"; lux[] = "ghi"; lux[] = "jkl";
lux[] = "mno"; lux[] = "pqrs"; lux[] = "tuv"; lux[] = "wxyz";
vector<string> ans;
if(digits.empty())
{
ans.push_back("");
return ans;
}
string X = digits;
vector<int> S(digits.length());
int k = ;
while(k >= )
{
while(k >= && S[k] < lux[digits[k] - ''].length())
{
X[k] = lux[digits[k] - ''][S[k]++];
if(k < digits.length() - )
{
k++;
}
else
{
ans.push_back(X); //当判断条件是 (长度 - 1) 时不需要 k-- 因为最后一位数字需要循环
}
}
S[k] = ;
k--;
}
if(ans.empty())
{
ans.push_back("");
return ans;
}
return ans;
}
};
【leetcode】 Letter Combinations of a Phone Number(middle)的更多相关文章
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【Leetcode】【Medium】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 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 ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【leetcode刷题笔记】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- Codeforces Round #270 1001
Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...
- 如何禁用wordpress的RSS Feed
RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,早期使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新.但随着采集技术的 ...
- 【C语言入门教程】4.2 二维数组
C 语言允许使用多维数组,即使用多组小标的数组,二维数组是最常用的多维数组.多维数组在内存中存放数据的顺序与一维数组相同,使用连续的存储单元. 4.2.1 二维数组的一般形式 二维数组的一般声明形式为 ...
- flexbox-CSS3弹性盒模型flexbox完整版教程
原文链接:http://caibaojian.com/flexbox-guide.html flexbox-CSS3弹性盒模型flexbox完整版教程 A-A+ 前端博客•2014-05-08•前端开 ...
- js网页中调用本地应用程序
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...
- 【转】 js怎么区分出点击的是鼠标左键还是右键?
IE 下 onMouseDown 事件有个 events.button 可以返回一个数值,根据数值判断取得用户按了那个鼠标键 events.button==0 默认.没有按任何按钮. events. ...
- 上传源码到github
到 http://mac.github.com/ 下载 github mac客户端 然后左上角 + 号, 点击add 添加 repository(代码仓库), 然后选择已有git项目, 然后点击右上角 ...
- 关于Promise:你可能不知道的6件事
FROM ME : 文章介绍了6个Promise的知识点: 1.then() 返回一个 forked Promise(分叉的 Promise):返回的有两种情况: 2.回调函数应该传递结果:在 pro ...
- 剑指Offer 反转链表
题目描述 输入一个链表,反转链表后,输出链表的所有元素. 思路: 法1:用栈,压栈出栈 法2:头插法(有递归非递归2中) AC代码: /* struct ListNode { int va ...
- MVC框架 与Smarty
MVC一种软件设计模式 MVC全名是 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据. ...