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;
    }
};
相关推荐
Mr_Xuhhh1 小时前
YAML相关
开发语言·python
阿巴~阿巴~2 小时前
JsonCpp:C++ JSON处理利器
linux·网络·c++·json·tcp·序列化和反序列化
Promise4852 小时前
贝尔曼公式的迭代求解笔记
笔记·算法
咖啡の猫2 小时前
Python中的变量与数据类型
开发语言·python
前端达人2 小时前
你的App消息推送为什么石沉大海?看Service Worker源码我终于懂了
java·开发语言
汤姆yu2 小时前
基于springboot的电子政务服务管理系统
开发语言·python
全栈师2 小时前
C#中控制权限的逻辑写法
开发语言·c#
fish_xk2 小时前
数据结构之二叉树中的堆
数据结构
S***q1922 小时前
Rust在系统工具中的内存安全给代码上了三道保险锁。但正是这种“编译期的严苛”,换来了运行时的安心。比如这段代码:
开发语言·后端·rust
打点计时器3 小时前
matlab 解决wfdb工具使用本地数据集报错
开发语言·matlab