Solution in C++


  • 0
    P
    class Solution 
    {
    public:
    	vector<string> letterCombinations(string digits) 
    	{
    
    		vector<string> ret;
    		if (digits.size() == 0) return ret;
    		vector<string> digToChar{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    		ret.push_back("");
    		for (int i = 0; i < digits.size(); ++i)
    		{
    			int len = ret.size();
    			for (int j = 1; j < digToChar[digits[i] - '0'].size(); ++j)
    			{
    				for (int k = 0; k < len; ++k)
    				{
    					ret.push_back(ret[k] + digToChar[digits[i] - '0'][j]);
    				}
    			}
    			for (int k = 0; k < len; ++k)
    			{
    				ret[k] = ret[k] + digToChar[digits[i] - '0'][0];
    			}
    		}
    		return ret;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.