easy to understand


  • 0
    Y

    [0_1497093191224_LeetCode_17_ LetterCombinationsofaPhoneNumber.cpp](Uploading 100%)


  • 0
    Y

    @YOYOLIEW said in easy to understand:

    [0_1497093191224_LeetCode_17_ LetterCombinationsofaPhoneNumber.cpp](Uploading 100%)

    class Solution1 {
    public:
    vector<string> letterCombinations(string digits) {
    vector<string> result;
    if (digits.empty()) return result;
    vector<string> tmp;
    tmp.resize(10);

    	tmp[0] = "";
    	tmp[1] = "";
    	tmp[2] = "abc";
    	tmp[3] = "def";
    	tmp[4] = "ghi";
    	tmp[5] = "jkl";
    	tmp[6] = "mno";
    	tmp[7] = "pqrs";
    	tmp[8] = "tuv";
    	tmp[9] = "wxyz";
    
    	int i = 0;
    	int num = 1;
    	string tmpdigits = "";
    	while (i < digits.size())
    	{
    		if (digits[i] == '9' || digits[i] == '7') 
    		{
    			num = 4 * num; tmpdigits = tmpdigits + digits[i];
    		}
    		else if (digits[i] == '1' || digits[i] == '0')
    		{
    			num = num;
    		}
    		else
    		{
    			num = 3 * num; tmpdigits = tmpdigits + digits[i];
    		}
    		i++;
    	}
    
    	result.resize(num);
    	for (int i = 0; i < num; i++)
    	{
    		(result[i]).resize(tmpdigits.size());
    	}
    
    	int len = tmpdigits.size() - 1;
    	int powtmp = 1;
    	int m = 0, n = 0;
    	for (int i = 0; i < tmpdigits.size(); i++)
    	{
    		powtmp = pow(4, n) * pow(3, m);
    		if ((int)tmpdigits[len] - 48 == 9 || (int)tmpdigits[len] - 48 == 7)
    		{
    			n++;
    			for (int j = 1; j <= num; j++)
    			{
    				if (j % (4 * powtmp) == 0 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][3]; continue; }
    				if (j % (4 * powtmp) <= 1 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][0]; continue; }
    				if (j % (4 * powtmp) <= 2 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][1]; continue; }
    				if (j % (4 * powtmp) <= 3 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][2]; continue; }
    				if (j % (4 * powtmp) <= 4 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][3]; continue; }
    			}
    			len--;
    		}
    		else
    		{
    			m++;
    			for (int j = 1; j <= num; j++)
    			{
    				if (j % (3 * powtmp) == 0 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][2]; continue; }
    				if (j % (3 * powtmp) <= 1 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][0]; continue; }
    				if (j % (3 * powtmp) <= 2 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][1]; continue; }
    				if (j % (3 * powtmp) <= 3 * powtmp) { result[j - 1][len] = tmp[(int)tmpdigits[len] - 48][2]; continue; }
    			}
    			len--;
    		}			
    	}
    	return result;
    }
    

    };


Log in to reply
 

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