A AC C++ solution


  • 0

    I could not find a good way to distinguish 'A' and 'Z' (since both of them can represent 26) -_-|||.
    So I obeyed a rule: if there is '@', change it into 'Z', and made the character before '@' -1. B@-> AZ,
    A@->@Z; delete character if '@' in the most left position.

    string convertToTitle(int n) 
    {
    	string ret;
    	int t;
    	while (n)
    	{
    		t = n % 26;
    		if (64 == ret[0])
    		{
    			ret[0] = 'Z';
    			--t;
    		}	
    		ret.insert(ret.begin(), 64 + t);
    		n /= 26;
    	}
    	if (64 == ret[0])
    		ret.erase(ret.begin());
    	return ret;
    }

Log in to reply
 

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