题目描述
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
解题思路
这题类比26进制,不同之处在于1-26而非0-25,所以每次求余之前先减一。
java代码
public String convertToTitle(int n) {
StringBuilder s = new StringBuilder();
while (n > 0){
s = s.append((char)(((n - 1) % 26) + 'A'));
n = (n - 1) / 26;
}
return s.reverse().toString();
}