1350. Excel Sheet Column Title

描述

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

相当于10进制转换26进制,但没有0,所以把n减一就更加方便了。

class Solution {
public:
    /**
     * @param n: a integer
     * @return: return a string
     */
    string convertToTitle(int n) {
        // write your code here
        if (n == 0) {
            return "";
        }
        return convertToTitle((n - 1) / 26) + (char)((n - 1) % 26 + 'A');
    }
};


猜你喜欢

转载自blog.csdn.net/vestlee/article/details/80718799