给你一个整数 columnNumber
,返回它在 Excel 表中相对应的列名称。
例如:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
示例 1:
输入:columnNumber = 1
输出:"A"
示例 2:
输入:columnNumber = 28
输出:"AB"
示例 3:
输入:columnNumber = 701
输出:"ZY"
示例 4:
输入:columnNumber = 2147483647
输出:"FXSHRXW"
提示:
-
1 <= columnNumber <= 231 - 1
public class Solution { //用周期法+进制规律 //假设num xxx为26进制 //10进制为x+x*26^1+x*26^2 public static String convertToTitle(int columnNumber) { //10进制成26 char[] arr=new char[]{'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z'}; if(columnNumber<26) { return Character.toString(arr[columnNumber-1]); }else { String s=""; while (columnNumber>26) { if(columnNumber%26==0) {//26的整数倍速 s='Z'+s; columnNumber=columnNumber/26-1; }else { //1-25 s=arr[columnNumber%26-1]+s; columnNumber=columnNumber/26; } } s=arr[columnNumber-1]+s; return s; } } public static void main(String[] args) { System.out.println(convertToTitle(26*26*26)); } }