当前位置:网站首页>168. excel table column name
168. excel table column name
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
168. Excel Table column name
Original title link :https://leetcode.cn/problems/excel-sheet-column-title/
Give you an integer columnNumber , Back to it in Excel The name of the corresponding column in the table .
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”
Example 4:
Input :columnNumber = 2147483647
Output :“FXSHRXW”
Their thinking :
Calculate the corresponding letter from low to high by calculating the remainder and quotient , here 26 The radix calculation is 0 To 25 This 26 Number , but A It's from 1 At the beginning , So when calculating the remainder and quotient , In order to be able to communicate with 26 The base number remains the same , So subtract it first 1, Calculate again .
Code implementation :
class Solution:
def convertToTitle(self, columnNumber: int) -> str:
ans = []
while columnNumber > 0:
# the reason being that 26 Base number :0~25, but A It's from 1 At the beginning , So the letters are relative to the normal 26 Decimal digit
# Added one more 1, So it's using 26 In hexadecimal calculation , First subtract 1 individual 1
columnNumber -= 1
# Calculate the remainder , The rightmost letter
cur = columnNumber % 26
# Convert letters
cur_ans = chr(cur + ord('A'))
ans.insert(0, cur_ans)
# Seeking quotient , Calculate the next letter
columnNumber //= 26
return ''.join(ans)
reference :
https://leetcode.cn/problems/excel-sheet-column-title/solution/excelbiao-lie-ming-cheng-by-leetcode-sol-hgj4/
边栏推荐
猜你喜欢

bootsrap中的栅格系统

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动

IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does

Home online shopping project

Server rendering technology JSP

TEC: Knowledge Graph Embedding with Triple Context

C语言的sem_t变量类型

LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表

【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹

SEM of C language_ Tvariable type
随机推荐
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
389. 找不同
Pyramid scene parsing network [pspnet] thesis reading
小程序容器技术与物联网IoT的结合点
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
LeetCode 128最长连续序列(哈希set)
Blueprism registration, download and install -rpa Chapter 1
318. 最大单词长度乘积
Gorilla/mux framework (RK boot): RPC error code design
Test function in pychram
详解Spark运行模式(local+standalone+yarn)
Promql select time series
30. 串联所有单词的子串
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
【快捷键】
Implement pow (x, n) function
Ouc2021 autumn - Software Engineering - end of term (recall version)
[TA frost wolf \u may - "hundred people plan"] 2.1 color space
【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型