[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语句。

目录
相关文章
|
5月前
|
Python
pandas 生成 Excel 时的 sheet 问题
pandas 生成 Excel 时的 sheet 问题
61 1
|
5月前
|
Python
Python:Pandas实现批量删除Excel中的sheet
Python:Pandas实现批量删除Excel中的sheet
217 0
|
9月前
|
easyexcel
【EasyExcel】第二篇:导出excel文件,导出多个sheet工作空间
【EasyExcel】第二篇:导出excel文件,导出多个sheet工作空间
|
9月前
|
存储 数据处理 Python
使用Python批量合并Excel文件的所有Sheet数据
使用Python批量合并Excel文件的所有Sheet数据
229 0
|
9月前
|
数据处理 Python
4种方法用Python批量实现多Excel多Sheet合并
4种方法用Python批量实现多Excel多Sheet合并
975 0
|
9月前
|
Java
|
9月前
|
easyexcel Java 数据库
excel多sheet页的导入
excel多sheet页的导入
168 0
|
2月前
|
存储 Java easyexcel
招行面试:100万级别数据的Excel,如何秒级导入到数据库?
本文由40岁老架构师尼恩撰写,分享了应对招商银行Java后端面试绝命12题的经验。文章详细介绍了如何通过系统化准备,在面试中展示强大的技术实力。针对百万级数据的Excel导入难题,尼恩推荐使用阿里巴巴开源的EasyExcel框架,并结合高性能分片读取、Disruptor队列缓冲和高并发批量写入的架构方案,实现高效的数据处理。此外,文章还提供了完整的代码示例和配置说明,帮助读者快速掌握相关技能。建议读者参考《尼恩Java面试宝典PDF》进行系统化刷题,提升面试竞争力。关注公众号【技术自由圈】可获取更多技术资源和指导。
|
2月前
|
数据采集 数据可视化 数据挖掘
利用Python自动化处理Excel数据:从基础到进阶####
本文旨在为读者提供一个全面的指南,通过Python编程语言实现Excel数据的自动化处理。无论你是初学者还是有经验的开发者,本文都将帮助你掌握Pandas和openpyxl这两个强大的库,从而提升数据处理的效率和准确性。我们将从环境设置开始,逐步深入到数据读取、清洗、分析和可视化等各个环节,最终实现一个实际的自动化项目案例。 ####
303 10
|
4月前
|
数据处理 Python
Python实用记录(十):获取excel数据并通过列表的形式保存为txt文档、xlsx文档、csv文档
这篇文章介绍了如何使用Python读取Excel文件中的数据,处理后将其保存为txt、xlsx和csv格式的文件。
265 3
Python实用记录(十):获取excel数据并通过列表的形式保存为txt文档、xlsx文档、csv文档

热门文章

最新文章