//注意【0123456789】中的0
public class Solution {
public String convertToTitle(int n) {
StringBuilder str = new StringBuilder();
int remainder=n;
while(remainder>26) {
if(remainder%26 == 0) {
str.insert(0,(char)('Z'));
remainder = remainder/26 -1;
} else {
str.insert(0,(char)(remainder%26-1+'A'));
remainder/=26;
}
}
if(remainder != 0)
str.insert(0,(char)(remainder-1+'A'));
return str.toString();
}
}
进制转换,注意n的开始范围,对26取模的范围是0-25,每一步n--,每一步都是一个新的子数,目的是0-25对应A-Z。
public class Solution {
public String convertToTitle(int n) {
StringBuilder str = new StringBuilder();
int remainder=n-1;
while(remainder>=26) {
str.insert(0,(char)(remainder%26+'A'));
remainder = remainder/26 - 1;
}
str.insert(0,(char)(remainder+'A'));
return str.toString();
}
}