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);                           
            }
        }      
    }    
};

 

posted @ 2013-09-03 12:56  代码改变未来  阅读(1017)  评论(0编辑  收藏  举报