Excell列標題

原題

  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

 

 

題目大意

  給定一個非正整數,返回他對應的Excel列標題。app

解題思路

  題目本質就是將10進制數轉換成26進制數,使用A-Z字母表示。spa

代碼實現

算法實現類.net

public class Solution {
    public String convertToTitle(int n) {

        char[] result = new char[20];
        int index = 20;
        n--;
        do {
            result[--index] = (char) ('A' + n % 26);
            n = n / 26 - 1;
        } while (n >= 0);
        return new String(result, index, 20 - index);
    }
}
相關文章
相關標籤/搜索