17. Letter Combinations of a Phone Number(第三周)
来源:互联网 发布:淘宝充值中心 编辑:程序博客网 时间:2024/06/05 07:20
这道题应用了深度遍历搜索的原理,进行了两个判断,如果达到了digits(eg.“234”)的大小,则将生成的字符串push进res中;首先将一个组合中的首字母push进out中,然后进行第二个组合的push,当达到了digits的大小,即所有字母组合都有字符push进了out,则将生成的字母组合push进res。这道题也运用了递归的思想。
代码如下:
class Solution {
public:vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits.empty()) return res;
string dict[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
letterCombinationsDFS(digits,dict,0,"",res);
return res;
}
void letterCombinationsDFS(string digits,string dict[],int level,string out,
vector<string> &res)
{
if(level == digits.size()) res.push_back(out);
else
{
string str = dict[digits[level] - '2'];
for(int i = 0;i<str.size();i++)
{
out.push_back(str[i]);
letterCombinationsDFS(digits,dict,level+1,out,res);
out.pop_back();
}
}
}
};
0 0
- 17. Letter Combinations of a Phone Number(第三周)
- 17.Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- string库之模拟实现strcpy与strncpy
- [ JS 进阶 ] 异步编程 promise模式 的简单实现
- FlowDroid 环境配置(mac OS)
- Android优化篇——网络连接
- nagios插件之监控linux下分区是否被挂载
- 17. Letter Combinations of a Phone Number(第三周)
- string库之模拟实现memcpy与memmove
- Oracle 中解析Json数据 PLJson的安装及使用
- Lua 笔记(第一天)
- 【J2EE】连接Mysql数据
- FFMPEG结构体分析:AVCodec
- 【C++】:C++泛型基础
- 【Leetcode】142. Linked List Cycle II
- git