【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. ...
随机推荐
- linux下查找包含BOM头的文件和清除BOM头命令
查找包含BOM头的文件,命令如下: grep -r -I -l $'^\xEF\xBB\xBF' ./ 这条命令会查找当前目录及子目录下所有包含BOM头的文件,并把文件名在屏幕上输出. 但 ...
- requests的安装与简单运用
requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: python的标准库urllib ...
- 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...
- Javascript面向对象编程一:基础篇
该随笔分为以下四部分: Javascript面向对象编程一:基础篇 Javascript面向对象编程二:封装 Javascript面向对象编程三:继承 Javascript面向对象编程四:控件 先弄个 ...
- 【C语言入门教程】5.6 函数库和文件
函数库是为代码复用建立的,将同一类型,需要在不同的程序里使用的函数放置在一起,就组成了一个函数库.如 C 语言的标准库,它集合了开发者常用的函数.开发者自行编写的函数也可以组成函数库,通常称之为自定义 ...
- Python制作统计图形
转载自:http://www.dcharm.com/?p=15 Python一般使用Matplotlib制作统计图形,用它自己的说法是‘让简单的事情简单,让复杂的事情变得可能’.(你说国外的“码农”咋 ...
- linux查找某一进程并杀死
1. 查找redis进程 ps -ef|grep redis-server 2.打印第二个参数,因为上面第二列是进程号 3.这两个进程号有一个是grep进程号,所以要去掉,反选 grep ps ...
- mysql解决自动断开8小时未曾用过的链接
今天有运维的同事反映,发布关键词不太稳定,点了没反应.就去线上看了一下日志,发现数据库没有链接,就查了一下问题 关于mysql自动断开的问题研究结果如下,在mysql中有相关参数设定,当数据库连接空闲 ...
- Java之enumeration(枚举)
enumeration(枚举)是JDK1.5引入的新特性,放在java.lang包中. 1.枚举类方法介绍 package com.enums; public class TestEnum { pub ...
- Codeforces 260 C. Boredom
题目链接:http://codeforces.com/contest/456/problem/C 解题报告:给出一个序列,然后选择其中的一个数 k 删除,删除的同时要把k - 1和k + 1也删除掉, ...