LeetCode: 168. Excel Sheet Column Title

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

Example 1:

Input: 1
Output: "A"

Example 2:

Input: 28
Output: "AB"

Example 3:

Input: 701
Output: "ZY"

解题思路

进制转换。本题实际是将十进制数转换为 26 进制数。

AC 代码

class Solution {
public:
    string convertToTitle(int n) {
        string ans;

        while(n)
        {
            ans.push_back((n-1)%26+'A');
            n = (n-1)/26;
        }

        reverse(ans.begin(), ans.end());

        return ans;
    }
};
全部评论

相关推荐

点赞 评论 收藏
分享
明天不下雨了:兄弟你是我今天看到的最好看的简历(我说的是简历风格跟简历书写)把985 211再搞亮一点。投boss就说;您好,我华科(985)研二在读,本科211。对您的岗位很感兴趣,希望能获得一次投递机会。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务