<17>——Letter Combinations of a Phone Number
来源:互联网 发布:it专业有哪些 编辑:程序博客网 时间:2024/06/04 20:42
17、Letter Combinations of a Phone Number
电话号码的字母组合
给定一个数字字符串,返回所有可能的字母组合,可以代表。
一个映射的数字字母(就像在电话里按钮)如下所示。
注意事项
以上的答案是按照词典编撰顺序进行输出的,不过,在做本题时,你也可以任意选择你喜欢的输出顺序。
样例
给定 "23"
返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
我的代码:
class Solution {//递归形式,时间复杂度(3^n),空间复杂度(n)public: vector<string> const T={"","","abc","def", "ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> letterCombinations(string digits) { vector<string> res; if(digits.empty())return res; dfs(res,"",0,digits); return res; } void dfs(vector<string> &res,string s,int rank,string digits) { if(rank==digits.length())res.push_back(s); else for(auto c:T[digits[rank]-'0']) dfs(res,s+c,rank+1,digits); }};
经典代码:
// LeetCode, Letter Combinations of a Phone Number// 时间复杂度O(3^n),空间复杂度O(1)class Solution {public: const vector<string> keyboard { " ", "", "abc", "def", // '0','1','2',... "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; vector<string> letterCombinations (const string &digits) { vector<string deep="9"> result(1, ""); for (auto d : digits) { const size_t n = result.size(); const size_t m = keyboard[d - '0'].size(); result.resize(n * m); for (size_t i = 0; i < m; ++i) copy(result.begin(), result.begin() + n, result.begin() + n * i); for (size_t i = 0; i < m; ++i) { auto begin = result.begin(); for_each(begin + n * i, begin + n * (i+1), [&](string &s) { s += keyboard[d - '0'][i]; }); } } return result; }};
阅读全文
0 0
- <17>——Letter Combinations of a Phone Number
- 17,Letter Combinations of a Phone Number
- #17 Letter Combinations of a Phone Number
- 17Letter 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
- LeetCode——Letter Combinations of a Phone Number
- leetcode 017 —— Letter Combinations of a Phone Number
- LeetCode题解——Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number —— Leetcode
- LeetCode——17. Letter Combinations of a Phone Number
- Leetcode——17. Letter Combinations of a Phone Number
- 算法系列——Letter Combinations of a Phone Number
- Algorithms—17.Letter Combinations of a Phone Number
- leetcode—Letter Combinations of a Phone Number
- Python爬虫实战(三):简单爬取网页图片
- Python中如何将String类型转换为变量
- 定位
- Face R-CNN
- 查看oracle指定的表哪些列使用索引
- <17>——Letter Combinations of a Phone Number
- bzoj 4145(状压dp)
- mongoose时间自动转化为格林尼治标准时间的解决方案
- 「VIM」基础操作
- windowser server 2012 r2 8080外网访问端口发布设置
- $.ajax提交时防止数据重复
- Tensorflow之MNIST解析
- 正则表达式验证是否为ip
- python(二)