LeetCode 168. Excel Sheet Column Title

因为这里没有用来表示0的字母,所以本来应当进位的地方用Z来代替,从而可以不进位,因此取余为0时要对高位减一,防止进位。

class Solution {
public:
    string convertToTitle(int n) {
        string res = "";
        string table[] ={"Z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y"};
        int t = n;
        while(t>0)
        {
            int temp = t%26;
            t/=26;
            if(temp==0) t--;
            res.append(table[temp]);
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

你可能感兴趣的:(LeetCode)