美文网首页
Leetcode 168. Excel Sheet Column

Leetcode 168. Excel Sheet Column

作者: 刘宇轩Freeman | 来源:发表于2017-05-10 14:51 被阅读0次

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

过程十分简单:

 string convertToTitle(int n) {
    string res;
    
    while(n > 0){
        n -- ;
        res = char('A' + n % 26) + res;
        n /= 26;
    }
    return res;
}

本质上是一个10进制转26进制的问题。
这个先减一十分重要。

相关文章

网友评论

      本文标题:Leetcode 168. Excel Sheet Column

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