LeetCode 17. Letter Combinations of a Phone Number高效解法
来源:互联网 发布:淘宝网天猫超市批发 编辑:程序博客网 时间:2024/06/07 03:55
/************************************************************************
* 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.
*
************************************************************************/
vector<string> letterCombinations(string digits) { vector<string> result; if (digits.empty()) return vector<string>(); static const vector<string> v = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; result.push_back(""); // add a seed for the initial case for (int i = 0; i < digits.size(); ++i) { int num = digits[i] - '0'; if (num < 0 || num > 9) break; const string& candidate = v[num]; if (candidate.empty()) continue; vector<string> tmp; for (int j = 0; j < candidate.size(); ++j) { for (int k = 0; k < result.size(); ++k) { tmp.push_back(result[k] + candidate[j]); } } result.swap(tmp); } return result;}
//test
int main(int argc, char**argv){ string s="123"; vector<string> ss = letterCombinations(s); printVector(ss); return 0;}
0 0
- LeetCode 17. Letter Combinations of a Phone Number高效解法
- Letter Combinations of a Phone Number 解法
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- leetcode Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- springMVC web项目 数据库用户名密码加密解密
- caffe源码解读
- 用JavaScript编写业务逻辑?
- UVA 11584 Partitioning by Palindromes
- 源码-印度式乘法口诀表(“双十九”)的PL/SQL实现
- LeetCode 17. Letter Combinations of a Phone Number高效解法
- ContextLoaderListener和ServletDispatcher
- 安卓时间相关的广播(Intent.ACTION_TIME_TICK)
- Qt学习之路(57): 文本文件读写
- POJ 2631 Roads in the North
- [从头读历史] 第301节 山海经 其实这不是一本神话书
- 查一年内的数据 SQL语句
- Android资源类型之Animation
- Oracle同义词创建及其作用