LeetCode: Excel Sheet Column Title

LeetCode: 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

/**
* Created by hzhou on 4/23/15. [email protected]
*/
public class ExcelSheetColumnTitle {
public String convertToTitle(int n) {
assert n > 0;
//StringBuilder sb = new StringBuilder();
String result = "";
while (n > 0) {
int mod = n % 26;
result = cal(mod) + result;
n = n / 26;
if (mod == 0) {
n--;
}
}
return result;
}
private char cal(int i) {
i = (i + 26 - 1) % 26;
return (char) ('A' + i);
}
}