1. 程式人生 > >LeetCode 168. Excel Sheet Column Title

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;
    }
};