-
Notifications
You must be signed in to change notification settings - Fork 1
/
168.excel表列名称.java
64 lines (61 loc) · 1.03 KB
/
168.excel表列名称.java
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
/*
* @lc app=leetcode.cn id=168 lang=java
*
* [168] Excel表列名称
*
* https://leetcode-cn.com/problems/excel-sheet-column-title/description/
*
* algorithms
* Easy (33.99%)
* Likes: 142
* Dislikes: 0
* Total Accepted: 14.4K
* Total Submissions: 40.8K
* Testcase Example: '1'
*
* 给定一个正整数,返回它在 Excel 表中相对应的列名称。
*
* 例如,
*
* 1 -> A
* 2 -> B
* 3 -> C
* ...
* 26 -> Z
* 27 -> AA
* 28 -> AB
* ...
*
*
* 示例 1:
*
* 输入: 1
* 输出: "A"
*
*
* 示例 2:
*
* 输入: 28
* 输出: "AB"
*
*
* 示例 3:
*
* 输入: 701
* 输出: "ZY"
*
*
*/
// @lc code=start
class Solution {
public String convertToTitle(int n) {
StringBuilder sb = new StringBuilder();
do {
int remainder = (n - 1) % 26;
n = (n - 1) / 26;
sb.insert(0, ((char) (remainder + 'A')));
} while (n != 0);
return sb.toString();
}
}
// @lc code=end