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
Source
public class Solution {
public String convertToTitle(int n) {
int num,le,k = n;
String res = new String();
while(k > 0){
num = k / 26;
if(k % 26 == 0){ //注意是26的倍数的情况
le = 26;
num--; //注意如AZ的情况,即52=1*26+26 而num在前面算出来是2 抛开个位上的Z 必须要减去一个26
}
else le = k % 26;
char b = (char)(le + 64);
res = b + res;
k = num;
}
return res;
}
}
测试
public static void main(String args[]){
int n = 26;
System.out.println(new Solution().convertToTitle(n));
}