lc-168


给你一个整数 columnNumber ,返回它在 Excel 表中相对应的列名称。

例如:

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

示例 1:

输入:columnNumber = 1
输出:"A"

示例 2:

输入:columnNumber = 28
输出:"AB"

示例 3:

输入:columnNumber = 701
输出:"ZY"

示例 4:

输入:columnNumber = 2147483647
输出:"FXSHRXW"

提示:

  • 1 <= columnNumber <= 231 - 1

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String convertToTitle(int columnNumber) {
        StringBuilder sBuilder = new StringBuilder();

        if (columnNumber <= 0) {
            return "";
        }

        int q = columnNumber;

        while (q > 0) {
            int rem = q % 26;
            q = q / 26;

            if (rem == 0) {
                sBuilder.append('Z');
                q -= 1;
            } else {
                sBuilder.append((char)('A' - 1 + rem));
            }
        }

        return sBuilder.reverse().toString();
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !