leetcode 17. Letter Combinations of a Phone Number
来源:互联网 发布:淘宝上买瓷砖靠谱吗 编辑:程序博客网 时间:2024/04/29 10:15
/*leetcode 17. Letter Combinations of a Phone NumberGiven 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"].参考:http://www.cnblogs.com/ganganloveu/p/4175384.html枚举所有情况对于每一个输入数字,对于已有的排列中每一个字符串,分别加入该数字所代表的每一个字符。所有是三重for循环。举例:初始化排列{""}1、输入2,代表"abc"已有排列中只有字符串"",所以得到{"a","b","c"}2、输入3,代表"def"(1)对于排列中的首元素"a",删除"a",并分别加入'd','e','f',得到{"b","c","ad","ae","af"}(2)对于排列中的首元素"b",删除"b",并分别加入'd','e','f',得到{"c","ad","ae","af","bd","be","bf"}(2)对于排列中的首元素"c",删除"c",并分别加入'd','e','f',得到{"ad","ae","af","bd","be","bf","cd","ce","cf"}参考 https://github.com/soulmachine/leetcode递归求解*/#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;class Solution {public: vector<string> letterCombinations(string digits) { vector<string> ret; if (digits == "") return ret; ret.push_back(""); vector<string> letters{ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; for (size_t i = 0; i < digits.size(); ++i) { int size = ret.size(); for (size_t j = 0; j < size; ++j) { string cur = ret[0]; //取出第一个string ret.erase(ret.begin()); //对于数字对应的字母来说,和cur相加,然后加入到数组中 for (size_t k = 0; k < letters[digits[i] - '0'].size(); ++k) ret.push_back(cur + letters[digits[i] - '0'][k]); } } return ret; } //递归解法 //由于长度固定为digits.size(),那么每次到了这个长度就压入 const vector<string> letters{ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; //digits为数字,cur为当前path的长度,path为找到的组合,reslut为结果数组 void dfs(const string& digits, size_t cur, string path, vector<string> result) { //当到了这个长度之后,说明path已经找到一个 if (cur == digits.size()) { result.push_back(path); return; } //如果没有,还要继续加入其他的字母。 for (auto c : letters[digits[cur] - '0']) dfs(digits, cur + 1, path + c, result); } vector<string> letterCombinations1(string digits) { vector<string> result; if (digits.empty()) return result; dfs(digits, 0, "", result); return result; }};void TEST(){ Solution sol; vector<string> ret; vector<string> ret1; ret = sol.letterCombinations("23"); ret1 = sol.letterCombinations1("23"); for (auto c : ret) cout << c << " "; cout << endl; for (auto c : ret1) cout << c << " "; cout << endl;}int main(){ TEST(); return 0;}
0 0
- 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
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- 2016普及组模拟考试 04题
- Linux学习---shell编程(09-sed)
- 百度开源深度学习平台Paddle
- mfc进阶1~mfc基本配置
- Linux学习---shell编程(10-字符处理命令)
- leetcode 17. Letter Combinations of a Phone Number
- SimpleAdapter
- http://www.chengxuyuan.com/post/1838.html?ref=myread
- leetcode 20. Valid Parentheses
- linux下源码安装cmake
- SQL大数据量查询的优化
- leetcode 064. Minimum Path Sum
- UE4之Behavior Tree介绍
- Linux学习---shell编程(11-条件判断式)