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;
    }
};
相关推荐
树下水月15 分钟前
python 连接hive2 数据库
开发语言·数据库·python
Tom4i17 分钟前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin
怕什么真理无穷17 分钟前
C++_面试15_零拷贝
c++·面试·职场和发展
凄戚20 分钟前
bash和命令
开发语言·chrome·bash
Evan芙23 分钟前
Bash 变量命名规则与类型使用
linux·运维·开发语言·chrome·bash
Espresso Macchiato24 分钟前
Leetcode 3748. Count Stable Subarrays
算法·leetcode·职场和发展·leetcode hard·leetcode 3748·leetcode周赛476·区间求和
AA陈超25 分钟前
ASC学习笔记0007:用于与GameplayAbilities系统交互的核心ActorComponent
c++·笔记·学习·ue5·虚幻引擎
大袁同学27 分钟前
【哈希hash】:程序的“魔法索引”,实现数据瞬移
数据结构·c++·算法·哈希算法·散列表
在人间负债^40 分钟前
Rust 实战项目:TODO 管理器
开发语言·后端·rust