美文网首页
168. Excel Sheet Column Title

168. Excel Sheet Column Title

作者: RobotBerry | 来源:发表于2017-05-06 16:34 被阅读0次

    问题

    Given a positive integer, return its corresponding column title as appear in an Excel sheet.

    例子

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB

    分析

    简单的字符映射,每次循环将数字除26减1然后求对26的余数再转成A-Z即可。

    要点

    字符映射,注意1、26、27等边界情况。

    时间复杂度

    O(logn)

    空间复杂度

    O(1)

    代码

    class Solution {
    public:
        string convertToTitle(int n) {
            string res;
            while (n--) {
                res = (char)('A' + n % 26) + res;
                n /= 26;
            }
            return res;
        }
    };
    

    相关文章

      网友评论

          本文标题:168. Excel Sheet Column Title

          本文链接:https://www.haomeiwen.com/subject/lrxytxtx.html