LeetCode:168. Excel表列名稱

這個題最小的數字不是從0開始,而是從1開始。所以如果要用進制轉換的思路來解決的話,在處理每一位的時候要把當前的位進行減1操作。
class Solution {
public:
    string convertToTitle(int n) {
        string res;
        while(n)
        {
            n-=1;
            int temp=n%26;
            res+=(char)('A'+temp);
            n/=26;
        }
        reverse(res.begin(),res.end());
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章