Letter Combinations of a Phone Number
Posted xpp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Letter Combinations of a Phone Number相关的知识,希望对你有一定的参考价值。
方法:使用递归的思想
这个问题与上一个问题的不同之处在于其在每个数字对应的字母中只能选择一个,这里的关键是对递归的过程有比较好的认识。
class Solution { public: const vector<string> keyboard{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> letterCombinations(string digits) { vector<string> result; if(digits.empty()) return result; dfs(digits, 0, "", result); return result; } void dfs(const string &digits, int cur, string path, vector<string> &result) { if(digits.size() == cur) { result.push_back(path); return; } for(int i=0; i <keyboard[digits[cur]-‘0‘].size(); ++i) dfs(digits, cur+1, path+keyboard[digits[cur]-‘0‘][i], result); } };
以上是关于Letter Combinations of a Phone Number的主要内容,如果未能解决你的问题,请参考以下文章
17. Letter Combinations of a Phone Number
[LeetCode]Letter Combinations of a Phone Number
Letter Combinations of a Phone Number
Leetcode 17. Letter Combinations of a Phone Number