-
Notifications
You must be signed in to change notification settings - Fork 0
/
168. Excel Sheet Column Title
66 lines (50 loc) · 1.28 KB
/
168. Excel Sheet Column Title
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
题目翻译:
给定一个整数columnNumber,返回它在Excel表中出现的相应列标题。
例如:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
class Solution {
// 定义方法,将整数转换为对应的Excel列标题
public String convertToTitle(int columnNumber) {
// 创建一个StringBuilder存储结果
StringBuilder sb = new StringBuilder();
// 当columnNumber大于0时,继续循环
while (columnNumber > 0) {
// 计算余数,注意需要先减一
int remainder = (columnNumber - 1) % 26;
// 将余数转换为字母,并加入结果字符串
sb.append((char)('A' + remainder));
// 计算商,注意需要先减一
columnNumber = (columnNumber - 1) / 26;
}
// 返回结果字符串,需要先反转再转化为字符串
return sb.reverse().toString();
}
}