Excell列标题

北城以北 提交于 2020-03-10 16:38:16

原题

  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列标题。

解题思路

  题目本质就是将10进制数转换成26进制数,使用A-Z字母表示。

代码实现

算法实现类

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);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!