LeetCode之17_Letter Combinations of a Phone Number
2016-06-26 14:08
399 查看
题目原文:
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.
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
题意分析:
电话机上一个数字按键对应了多个字符,给出一个数字串,给出所有可能的字符串组合。
将对应关系放入map中后,直接深度搜索即可。
解题代码:
//Input:Digit string "23"
//Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
//通过输入的数字,输出可能的字母组合
//遍历所有情况,因为深度不确定,考虑使用递归,进行深度搜索
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
map<char,string> num2Str;
char *pstr;
vector<string> vecBack;
vector<string> letterCombinations(string digits) {
vecInit(num2Str);
vecBack.clear();
pstr = (char*)malloc((1+digits.length())*sizeof(char));
if (pstr == NULL)
{
return vecBack;
}
if (digits.length()>0)
{
PushLetter(digits,0);
}
return vecBack;
}
void PushLetter(string digits, int Loc)
{
char szNum = digits[Loc];
string szPoss = num2Str[szNum];
for (int i=0; i<szPoss.length(); i++)
{
pstr[Loc] = szPoss[i];
if (Loc+1 < digits.length())
{
PushLetter(digits,Loc+1);
}
else if (Loc+1 == digits.length())
{
pstr[Loc+1] = '\0';
string sRet = pstr;
vecBack.push_back(sRet);
}
}
}
//初始化map中的对象
void vecInit(map<char,string> &vecInput)
{
vecInput.insert(make_pair('1',"@"));
vecInput.insert(make_pair('2',"abc"));
vecInput.insert(make_pair('3',"def"));
vecInput.insert(make_pair('4',"ghi"));
vecInput.insert(make_pair('5',"jkl"));
vecInput.insert(make_pair('6',"mno"));
vecInput.insert(make_pair('7',"pqrs"));
vecInput.insert(make_pair('8',"tuv"));
vecInput.insert(make_pair('9',"wxyz"));
vecInput.insert(make_pair('0',"_"));
vecInput.insert(make_pair('*',"+"));
//vecInput.insert(make_pair(''))
}
};
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.
题意分析:
电话机上一个数字按键对应了多个字符,给出一个数字串,给出所有可能的字符串组合。
将对应关系放入map中后,直接深度搜索即可。
解题代码:
//Input:Digit string "23"
//Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
//通过输入的数字,输出可能的字母组合
//遍历所有情况,因为深度不确定,考虑使用递归,进行深度搜索
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
map<char,string> num2Str;
char *pstr;
vector<string> vecBack;
vector<string> letterCombinations(string digits) {
vecInit(num2Str);
vecBack.clear();
pstr = (char*)malloc((1+digits.length())*sizeof(char));
if (pstr == NULL)
{
return vecBack;
}
if (digits.length()>0)
{
PushLetter(digits,0);
}
return vecBack;
}
void PushLetter(string digits, int Loc)
{
char szNum = digits[Loc];
string szPoss = num2Str[szNum];
for (int i=0; i<szPoss.length(); i++)
{
pstr[Loc] = szPoss[i];
if (Loc+1 < digits.length())
{
PushLetter(digits,Loc+1);
}
else if (Loc+1 == digits.length())
{
pstr[Loc+1] = '\0';
string sRet = pstr;
vecBack.push_back(sRet);
}
}
}
//初始化map中的对象
void vecInit(map<char,string> &vecInput)
{
vecInput.insert(make_pair('1',"@"));
vecInput.insert(make_pair('2',"abc"));
vecInput.insert(make_pair('3',"def"));
vecInput.insert(make_pair('4',"ghi"));
vecInput.insert(make_pair('5',"jkl"));
vecInput.insert(make_pair('6',"mno"));
vecInput.insert(make_pair('7',"pqrs"));
vecInput.insert(make_pair('8',"tuv"));
vecInput.insert(make_pair('9',"wxyz"));
vecInput.insert(make_pair('0',"_"));
vecInput.insert(make_pair('*',"+"));
//vecInput.insert(make_pair(''))
}
};
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解