您的位置:首页 > 其它

leetcode---letter-combinations-of-a-phone-number---dfs

2018-01-21 21:08 375 查看
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.

class Solution {
public:
void dfs(int dep, string tmp, vector<string> &ans, int n, string digits, string s[])
{
if(dep >= n)
{
ans.push_back(tmp);
}

int k = digits[dep] - '0';
for(int j=0; j<s[k].size(); j++)
dfs(dep+1, tmp+s[k][j], ans, n, digits, s);
}

vector<string> letterCombinations(string digits)
{
string s[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> ans;
int n = digits.size();
string tmp = "";
dfs(0, tmp, ans, n, digits, s);
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: