Leetcode 168. Excel Sheet Column Title

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

思路:可以看做是一个十进制到二十六进制的转换问题。

public String convertToTitle(int n) {
    if (n <= 0) {
        return "";
    }

    
    StringBuilder res = new StringBuilder();
    while (n > 0) {
        n--;
        int mode = n % 26;
        res.insert(0, (char)('A' + mode));
        n /= 26;
    }

    return res.toString();
}
    原文作者:ShutLove
    原文地址: https://www.jianshu.com/p/7e2b86250cc0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞