【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】

【168-Excel Sheet Column Title(Excell列标题)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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);
    }
}

评測结果

  点击图片。鼠标不释放,拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47856075

时间: 2024-10-11 06:29:27

【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】的相关文章

168.Excel Sheet Column Title

Excel Sheet Column Title 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 注意:由于下标从1开始而不是从0开始,因此要减一操作. static public string Convert

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

题目: 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 翻译: 给定一个正数,返回它类似Excle中对应的列标题. 分析: 关联问题:"Excel Sheet Column Number"

Java for LeetCode 168 Excel Sheet Column Title

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 解题思路: JAVA实现如下: static public String convertToTitle(int n) { S

Leetcode 168 Excel Sheet Column Title

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

【LeetCode】168 - Excel Sheet Column Title

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 Solution: 由于A->1,  计算s的末尾时需先减去1 1 class Solution { 2 public: 3 string convert

【LeetCode】168. Excel Sheet Column Title 解题小结

题目: 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 简单题 class Solution { public: string convertToTitle(int n) { string str; char

168. Excel Sheet Column Title (Easy)

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 思路:1.进制转化,将十进制转化为每位以A-Z表示的26进制数:2.Python中,使用ord()函数将字母转化为整数,使用chr()函数将整数转化回字母:3.

168. Excel Sheet Column Title (Math)

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 class Solution { public: string convertToTitle(int n) { //resi

Leetcode 168 Excel Sheet Column Title 进制数转化

题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- 1 class Solution { 2 public: 3 string convertToTitle(int n) { 4 string s(""); 5 for(; n--; n/=26){ 6 s += string("A"); 7 s[s.size() - 1] += n% 26; 8 } 9 reverse(s.begi

168 Excel Sheet Column Title Excel表列名称

给定一个正整数,返回它在Excel表中相对应的列名称.示例:    1 -> A    2 -> B    3 -> C    ...    26 -> Z    27 -> AA    28 -> AB 详见:https://leetcode.com/problems/excel-sheet-column-title/description/ class Solution { public: string convertToTitle(int n) { string