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;
    }
};
相关推荐
qq_406176145 分钟前
JavaScript中的循环特点和区别
开发语言·javascript·ecmascript
月明长歌5 分钟前
【码道初阶】【LeetCode 160】相交链表:让跑者“起跑线对齐”的智慧
java·算法·leetcode·链表
beordie.cloud9 分钟前
LeetCode 49. 字母异位词分组 | 从排序到计数的哈希表优化之路
算法·leetcode·散列表
我命由我1234510 分钟前
Python 开发 - OpenAI 兼容阿里云百炼平台 API
开发语言·人工智能·后端·python·阿里云·ai·语言模型
GokuCode13 分钟前
【GO高级编程】02.GO接收者概述
开发语言·后端·golang
Aotman_14 分钟前
JavaScript去除对象字段空格
开发语言·前端·javascript
共享家952715 分钟前
每日一题(一)
算法
云和数据.ChenGuang18 分钟前
Zabbix 6 与 PHP 5 版本**完全不兼容
运维·开发语言·php·zabbix·运维工程师
csbysj202018 分钟前
Ruby 范围(Range)
开发语言