【second】Letter Combinations of a Phone Number

 

    vector<string> letterCombinations(string digits) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        string maps[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        vector<string> res;
        dfs(0,digits,"",res,maps);
        return res;
    }

    void dfs(int pos,string& digits,string str,vector<string>& res,string maps[])
    {
        if(pos==digits.size())
        {
            res.push_back(str);
            return;
        }
        
        int num = digits[pos] - '2';
        for(int i=0;i<maps[num].size();i++)
            dfs(pos+1,digits,str+maps[num][i],res,maps);
    }

  

posted @ 2013-10-23 20:02  summer_zhou  阅读(148)  评论(0编辑  收藏  举报