LeetCode168. Excel Sheet Column Title

文章目录

一、题目

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

A -> 1

B -> 2

C -> 3

...

Z -> 26

AA -> 27

AB -> 28

...

Example 1:

Input: columnNumber = 1

Output: "A"

Example 2:

Input: columnNumber = 28

Output: "AB"

Example 3:

Input: columnNumber = 701

Output: "ZY"

Constraints:

1 <= columnNumber <= 231 - 1

二、题解

cpp 复制代码
class Solution {
public:
    string convertToTitle(int columnNumber) {
        string res;
        while (columnNumber > 0) {
            int a0 = (columnNumber - 1) % 26 + 1;
            res += a0 - 1 + 'A';
            columnNumber = (columnNumber - a0) / 26;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};
相关推荐
WangMing_X18 分钟前
C#实现动态验证码生成器:安全防护与实际应用场景
开发语言·安全·c#·验证码·图片
m0_5557629019 分钟前
qt designer中的Spacer相关设置
服务器·开发语言·qt
jk_1011 小时前
MATLAB中enumeration函数用法
开发语言·matlab
吧啦吧啦吡叭卜1 小时前
【打卡d5】快速排序 归并排序
java·算法·排序算法
L_cl1 小时前
【Python 数据结构 15.哈希表】
数据结构·算法·散列表
肖筱小瀟2 小时前
2025-3-13 leetcode刷题情况(贪心算法--区间问题)
算法·leetcode·贪心算法
十年一梦实验室2 小时前
C++ 中的 RTTI(Run-Time Type Information,运行时类型识别)
开发语言·c++
纽约恋情2 小时前
C++——STL 常用的排序算法
开发语言·c++·排序算法
肖筱小瀟2 小时前
2025-3-12 leetcode刷题情况(贪心算法--区间问题)
算法·leetcode·贪心算法