Question
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
Java Code
public String convertToTitle(int n) {
StringBuffer title = new StringBuffer();
int remainder;
while(n != 0) {
remainder = n % 26;
title.insert(0, (char)('A' + remainder - 1));
//注意26进制的数字取值范围为[0,25],但是题目中的取值范围为[1,26]
n = (n-1) / 26;
}
return title.toString();
}
说明
- 本题的实质是将10进制转成26进制