Excel Sheet Column Title (STRING - TYPE CONVERTION)

QUESTION

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 

FIRST TRY

class Solution {
public:
    string convertToTitle(int n) {
        int remain = n%26;
        n /= 26;
        string ret = "";
        char ch;
        while(1)
        {
            if(n == 0 && remain == 0)
            {
                return ret;
            }
            else if(n == 0)
            {
                ch = getChar(remain);
                return ch + ret;
            }
            ch = getChar(remain);
            ret = ch + ret; //char -> string, direct concat
            remain = n%26;
            n /= 26;
        }
        return ret;
    }

    char getChar(int n)
    {
        if(n != 0)
            return ‘A‘+ n - 1;
        else
            return ‘Z‘;
    }
};

Result: Wrong

Input: 26
Output: "AZ"
Expected: "Z"

SECOND TRY

注意了余数为0的情况

class Solution {
public:
    string convertToTitle(int n) {
        int remain = n%26;
        n /= 26;
        string ret = "";
        char ch;
        while(1)
        {
            if(n == 0 && remain == 0)
            {
                return ret;
            }
            else if(n == 0)
            {
                ch = getChar(n,remain);
                return ch + ret;
            }
            ch = getChar(n,remain);
            ret = ch + ret; //char -> string, direct concat
            remain = n%26;
            n /= 26;
        }
        return ret;
    }

    char getChar(int& n, int remain)
    {
        if(remain != 0)
            return ‘A‘+ remain - 1;
        else
        {
            n -= 1;
            return ‘Z‘;
        }
    }
};
时间: 2024-10-04 17:52:40

Excel Sheet Column Title (STRING - TYPE CONVERTION)的相关文章

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

2016.5.19——Excel Sheet Column Title

Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[i]-'A'+1.(为什么有+1,-1还有点迷糊,貌似是十进制是0-9,26进制是) 2.十进制到26进制转化 题目: Given a positive integer, return its corresponding column title as appear in an Excel shee

Excel Sheet Column Number & Excel Sheet Column Title

1. Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 1 class S

leetcode Excel Sheet Column Title python

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 python code class Solution: # @param {integer} n # @ret

[leetcode] Excel Sheet Column Title & Excel Sheet Column Number

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 思路: 10进制转26进制 .先求低位再求高位,与10进制转2进制一样. 题解: class Solution

leetcode_168题——Excel Sheet Column Title(数学问题)

Excel Sheet Column Title Total Accepted: 25652 Total Submissions: 142582My Submissions Question Solution 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

LeetCode168——Excel Sheet Column Title

LeetCode168--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

[LeetCode]62. Excel Sheet Column Title Excel列序号

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] Excel Sheet Column Title 求Excel表列名称

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