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;
    }
};
相关推荐
长不胖的路人甲17 分钟前
什么是赫夫曼树(哈夫曼树 / Huffman Tree)
python·算法·霍夫曼树
choumin21 分钟前
创建型模式——原型模式
c++·设计模式·原型模式·创建型模式
bksczm43 分钟前
Linux之日志和线程池、内存池
java·开发语言
笨蛋不要掉眼泪1 小时前
Java虚拟机:对象复活、引用强度与Stop-The-World
java·开发语言·jvm
布朗克1681 小时前
Go 入门到精通-33-unsafe 与 CGO
开发语言·后端·golang·unsafe·cgo
Warren2Lynch1 小时前
掌握 UML 构造型、标记定义与标记值:面向领域特定建模的 UML 扩展全面指南
大数据·算法·uml
157092511342 小时前
【无标题】
开发语言·python·算法
稚南城才子,乌衣巷风流2 小时前
换根法(Rerooting)算法详解
算法
xieliyu.2 小时前
MySQL 存储过程详解:概念、创建与删除全教程
开发语言·数据库·mysql
晓子文集2 小时前
Tushare接口文档:期货交易日历(fut_trade_cal)
大数据·算法