My C++ code 0ms


  • 0
    P
    class Solution {
    public:
        vector<string> letterCombinations(string digits) {
    	vector<string> vecResult;
    	if (digits.length() <= 0)
    	{
    		return vecResult;
    	}
    	string strIniArray[10] = { " ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
    	vector<int> vecDigitIndexs(digits.length(), 0);
    	while (true)
    	{
    		string strResult = "";
    		for (size_t i = 0; i < digits.length(); ++i)
    		{
    			strResult += strIniArray[digits[i] - 48][vecDigitIndexs[i]];
    		}
    		vecResult.push_back(strResult);
    
    		int nIndex = digits.length() - 1;
    		while (nIndex >= 0)
    		{
    			vecDigitIndexs[nIndex] += 1;
    			if (vecDigitIndexs[nIndex] >= strIniArray[digits[nIndex] - 48].length())
    			{
    				vecDigitIndexs[nIndex] = 0;
    				--nIndex;
    			}
    			else
    				break;
    		}
    		if (nIndex < 0)
    		{
    			break;
    		}
    	}
    
    	return vecResult; 
        }
    };

Log in to reply
 

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