Convert a number to an Excel column, and convert an Excel column back to a number.
In Excel, columns are labeled as:
- 1 →
"A" - 2 →
"B" - …
- 26 →
"Z" - 27 →
"AA" - 28 →
"AB" - …
You are asked to implement two functions:
- Given a positive integer, convert it to its corresponding Excel column title (e.g.,
1 -> "A",28 -> "AB"). - Given an Excel column title (string of uppercase letters), convert it back to its corresponding number (e.g.,
"A" -> 1,"AB" -> 28).
You may assume:
- The integer input is positive
- The column title consists of only uppercase letters
A–Z
- 数字 → 列标:当成“ 没有 0 的 26 进制 ”,每次先
n--再取(n % 26)映射到A..Z,然后n /= 26,拼出字符串并反转。 - 列标 → 数字:按普通 26 进制处理,从左到右:
res = res * 26 + (字母值),其中A=1,B=2…Z=26。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。
正文完