LeetCode[Math]----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 

分析:

给一个正整数,返回对应的Excel中列的表示。这种表示是用字母A-Z来表示的,以26为进制。

类比十进制,我们递归的来处理给定的数字num。首先,如果num是属于1-26,那么直接返回这个数字对应的字符即可;如果它大于26,那么这个num的表示char可以表示为两部分prefix和endfix,endfix为num%26对应的字符表示,而prefix为num/26的字符表示。

对于26进制,在当num可以被26整除时,余数为0,而此时的endfix表示应该为Z,而prefix应为(num/26)-1对应的字符。

代码为:

class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        if 1 <= n <= 26:
            return chr(64 + n)
        if n % 26 == 0:
            return self.convertToTitle((n / 26) - 1) + 'Z'
        return self.convertToTitle(n / 26) + self.convertToTitle(n % 26)

非递归代码为:

class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        char = ''
        while n:
            char = chr((n-1) % 26 + 65) + char
            n = (n-1) / 26
        return char

--------------------------------------------------------------------------------------

分割线,下面是上面一题的逆转换。

Excel Sheet Column Number

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 

分析:

给定相应的字符表示,还原出对应的数字。

原串的表示仍然是通过26进制生成的,类似于10进制,数字每增加一位x,表示值增加了x*(10^y),y为x在数字中的第y位(从0开始)。比如十进制abc=a*10^2 + b*10^1 + c*10^0。同样的给定26进制表示的字符串如ABC=A*26^2 + B*26^1
+ C*26^0=1*26^2 + 2*26^1 + 3*26^0=731.

对应的代码为:

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        if len(s) == 1:
            return ord(s) - 64
        return self.titleToNumber(s[-1]) + self.titleToNumber(s[:-1]) * 26

非递归代码为:

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        num = 0
        while s:
            num = num * 26 + ord(s[0]) - 64
            s = s[1:]
        return num
时间: 2024-11-14 13:21:58

LeetCode[Math]----Excel Sheet Column Title----Excel Sheet Column Number的相关文章

[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 &amp; 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】Excel Sheet Column Title &amp; Excel Sheet Column Number (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 思路: 相当于10进制转26进制.与一般不一样的是10进制对应的是0 - 9.而这个26进制对应的是 A(1)- Z(26), 没有0. 我的代码: strin

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

Excel Sheet Column Number &amp; 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][JavaScript]Excel Sheet Column Title

https://leetcode.com/problems/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 我恨进制转换,傻傻算不清. /** * @par

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】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:经过分析不难发现实际上就是数对于 26 进制的转换用字母表示出来.代码如下: 1 class Solution: 2 # @param {in

leetCode(66)-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对应的该是第二个字母B,然而对应A,进位之后应该是BA,实际是AA,由此判断字母也是26进制,只是始终

【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