[LeetCode]--168. Excel Sheet Column Title

简介: Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB Credits:

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 

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.

本质上就是将一个10进制数转换为一个26进制的数

注意:由于下标从1开始而不是从0开始,因此要减一操作。

The Best Answer:

/**
     * 最好的方式
     */
    public String convertToTitle2(int n) {
        String ret = "";
        while (n != 0) {
            ret = (char) ((n - 1) % 26 + 'A') + ret;
            n = (n - 1) / 26;
        }
        return ret;
    }

再提供一种递归一种非递归的方式,都是Accept!

递归形式:

/**
     * 递归形式
     * 
     * @param n
     * @return
     */
    public String convertToTitle(int n) {
        if (n == 0)
            return "";
        if (n <= 26)
            return intToStr(n);
        String str = "";
        int temp = n % 26;
        int i = n / 26;
        if (temp == 0)
            i--;
        str += convertToTitle(i);
        str += intToStr(temp);
        return str;
    }

    public String intToStr2(int x) {
        switch (x) {
        case 0:
            return "A";
        case 1:
            return "B";
        case 2:
            return "C";
        case 3:
            return "D";
        case 4:
            return "E";
        case 5:
            return "F";
        case 6:
            return "G";
        case 7:
            return "H";
        case 8:
            return "I";
        case 9:
            return "J";
        case 10:
            return "K";
        case 11:
            return "L";
        case 12:
            return "M";
        case 13:
            return "N";
        case 14:
            return "O";
        case 15:
            return "P";
        case 16:
            return "Q";
        case 17:
            return "R";
        case 18:
            return "S";
        case 19:
            return "T";
        case 20:
            return "U";
        case 21:
            return "V";
        case 22:
            return "W";
        case 23:
            return "X";
        case 24:
            return "Y";
        case 25:
            return "Z";
        }
        return "";
    }

非递归形式:

/**
     * 非递归形式
     * 
     * @param n
     * @return
     */
    public String convertToTitle1(int n) {
        if (n == 0)
            return "";
        if (n <= 26)
            return intToStr(n);
        String str = "";
        int temp = 0;
        while (n != 0) {
            temp = (n - 1) % 26;
            str = intToStr2(temp) + str;
            n = (n - 1) / 26;
        }
        return str;
    }

public String intToStr2(int x) {
        switch (x) {
        case 0:
            return "A";
        case 1:
            return "B";
        case 2:
            return "C";
        case 3:
            return "D";
        case 4:
            return "E";
        case 5:
            return "F";
        case 6:
            return "G";
        case 7:
            return "H";
        case 8:
            return "I";
        case 9:
            return "J";
        case 10:
            return "K";
        case 11:
            return "L";
        case 12:
            return "M";
        case 13:
            return "N";
        case 14:
            return "O";
        case 15:
            return "P";
        case 16:
            return "Q";
        case 17:
            return "R";
        case 18:
            return "S";
        case 19:
            return "T";
        case 20:
            return "U";
        case 21:
            return "V";
        case 22:
            return "W";
        case 23:
            return "X";
        case 24:
            return "Y";
        case 25:
            return "Z";
        }
        return "";
    }

自然没有第一种解决巧妙,直接用余数+’A’,这样就可以直接得出需要的结果字符,而不需要用数组或者这种长长的switch语句。

目录
相关文章
|
8天前
|
easyexcel
【EasyExcel】第二篇:导出excel文件,导出多个sheet工作空间
【EasyExcel】第二篇:导出excel文件,导出多个sheet工作空间
|
8天前
|
存储 数据处理 Python
使用Python批量合并Excel文件的所有Sheet数据
使用Python批量合并Excel文件的所有Sheet数据
41 0
|
8天前
|
数据处理 Python
4种方法用Python批量实现多Excel多Sheet合并
4种方法用Python批量实现多Excel多Sheet合并
81 0
|
7月前
【Leetcode-171.Excel表列序号 -168.Excel表列名称】
【Leetcode-171.Excel表列序号 -168.Excel表列名称】
26 0
|
8天前
|
Java
|
8天前
|
easyexcel Java 数据库
excel多sheet页的导入
excel多sheet页的导入
|
8天前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 171. Excel 表列序号 算法解析
☆打卡算法☆LeetCode 171. Excel 表列序号 算法解析
|
8天前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 168. Excel表列名称 算法解析
☆打卡算法☆LeetCode 168. Excel表列名称 算法解析
|
5月前
|
索引
POI(excel)中WorkBook和Sheet应用实践总结
POI(excel)中WorkBook和Sheet应用实践总结
94 1
|
6月前
excel函数调用其他sheet单元格
excel函数调用其他sheet单元格

热门文章

最新文章