原题链接在这里: https://leetcode.com/problems/excel-sheet-column-title/
刚开始以为是简单地26进制,结果有个特殊情况就是"Z", n=26, return "Z"; n = 52, return "AZ". 当current digit 是26时,要把当前位increase 26,并且总体的值要减去26,因为when n = 52, it should return "AZ" but not "BZ".
AC Java:
public class Solution {
public String convertToTitle(int n) {
if(n<=0)
return "";
StringBuilder sbStr = new StringBuilder();
int lastDigit;
char lastChar;
while(n>0){
lastDigit = n%26; //error
if(lastDigit == 0){
lastDigit += 26;
n-=26;
}
lastChar = (char)(lastDigit+64);
sbStr.insert(0,lastChar);
n = n/26;
}
return sbStr.toString();
}
}
与Excel Sheet Column Number呼应。