-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelSheetColumnTitle.java
45 lines (42 loc) · 1 KB
/
ExcelSheetColumnTitle.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
package easy;
/**
* Given a positive integer, return its corresponding column title as appear in an Excel sheet.
* For example:
* 1 -> A
* 2 -> B
* 3 -> C
* ...
* 26 -> Z
* 27 -> AA
* 28 -> AB
*
* 解决思路:
* 递归 n/26 , n %= 26
* 被整除是特殊情况
*
* Created by second on 2017/10/2
*/
public class ExcelSheetColumnTitle {
public String convertToTitle(int n) {
String str = "";
if (n > 26){
if (n % 26 == 0){
if (n / 26 - 1 > 26)
str += convertToTitle(n / 26 - 1);
else
str += (char) ('A' + (n / 26 - 1) - 1);
n = 26;
}else{
if (n / 26 > 26)
str += convertToTitle(n / 26);
else
str += (char)('A' + n / 26 - 1);
n %= 26;
}
str += convertToTitle(n);
}else {
str += (char) ('A' + n - 1);
}
return str;
}
}