分析
难度 易
来源
https://leetcode.com/problems/excel-sheet-column-title/
十进制转26进制
注意,z表示26,而非0
题目
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”
解答
package LeetCode;
public class L168_ExcelSheetColumnTitle {
public String convertToTitle(int n) {
StringBuilder sb=new StringBuilder();
while(n>0){
if(n%26!=0){
sb.append((char)(n%26+64));
n/=26;
}
else{
sb.append('Z');
n=n/26-1;
}
}
return sb.reverse().toString();
}
public static void main(String[] args){
L168_ExcelSheetColumnTitle l168=new L168_ExcelSheetColumnTitle();
System.out.println(l168.convertToTitle(52));
}
}