168. Excel Sheet Column Title

Problem:

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 
    ...

Example 1:

Input: 1
Output: "A"

Example 2:

Input: 28
Output: "AB"

Example 3:

Input: 701
Output: "ZY"

思路

Solution (C++):

string convertToTitle(int n) {
    vector<int> bit;
    string ans = "";
    while (n) {
        bit.push_back(n%26);
        n /= 26;
    }
    int len = bit.size();
    for (int i = 0; i < len-1; ++i) {
        if (bit[i] == 0) {
            bit[i] += 26;
            --bit[i+1];
        }
    }
    for (int i = len-1; i >= 0; --i) {
        if (bit[i] != 0)
            ans += ('A' + bit[i] - 1);
    }
    return ans;
}

性能

Runtime: 0 ms  Memory Usage: 6.3 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

原文链接: https://www.cnblogs.com/dysjtu1995/p/12641372.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    168. Excel Sheet Column Title

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/340316

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午12:32
下一篇 2023年3月2日 上午12:32

相关推荐