Leetcode Letter Combinations of a Phone Number
dfs
class Solution {
public:
string str[8]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string>s;
vector<string> letterCombinations(string digits)
{
s.clear();
dfs(digits,"");
return s;
}
void dfs(string digits,string temp)
{
if(digits.size()==0)
{
if(find(s.begin(),s.end(),temp)==s.end())
s.push_back(temp);
}
else
{
for(int j=0;j<str[digits[0]-'2'].size();j++)
{
temp+=str[digits[0]-'2'][j];
dfs(digits.substr(1,digits.size()),temp);
temp=temp.substr(0,temp.size()-1);
}
}
}
};
浙公网安备 33010602011771号