LeetCode(168) Excel Sheet Column Title

1195 ワード

タイトルは以下の通りです.
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example:     1 -> A     2 -> B     3 -> C     ...     26 -> Z     27 -> AA     28 -> AB 
次のように分析されます.
注意変換は1からカウントされ、0から始まるものではないので、26進変換を直接するほど簡単ではなく、中間的にいくつかの変換が必要です.26-Z,52->AZなど何度か試して、正しいコードを試しました.この問題と相反する.
マイコード:
class Solution {    
public:
    string convertToTitle(int n) {
        string array[26] = {"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", "Z"};
        string result = "";
        string each_digit = "";
        while (n > 0) {
            int digit = n%26;
            if (digit == 0) digit = 26;
            each_digit = array[digit - 1];
            result = each_digit + result;
            n = (n - 1)/26;  //n    1,     26,      AZ.
        }
        return result;
    }
};