题目描述:
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 ...
Example 1:
Input: 1 Output: "A"
Example 2:
Input: 28 Output: "AB"
Example 3:
Input: 701 Output: "ZY"
中文理解:将一个整数转化为A-Z的组成的字符串表示。
解题思路:这题实际是将十进制转换为1-26的26进制,和我们一般见到的进制转换不同的是进制不是从0开始的到N-1结束的,故这题稍微需要修改一下思路,若n%26==0,则为Z,且n-=26。
代码(java):
class Solution {
public String convertToTitle(int n) {
StringBuffer res=new StringBuffer();
while(n>0){
int mod=n%26;
if(mod==0){
res.insert(0,'Z');
n-=26;
}
else res.insert(0,(char)('A'+mod-1));
n=n/26;
}
return res.toString();
}
}