Skip to content

Latest commit

 

History

History
59 lines (43 loc) · 1.58 KB

168.md

File metadata and controls

59 lines (43 loc) · 1.58 KB

Single Number II

Description

link


Solution

A   1     AA    26+ 1     BA  2×26+ 1     ...     ZA  26×26+ 1     AAA  1×26²+1×26+ 1
B   2     AB    26+ 2     BB  2×26+ 2     ...     ZB  26×26+ 2     AAB  1×26²+1×26+ 2
.   .     ..    .....     ..  .......     ...     ..  ........     ...  .............
.   .     ..    .....     ..  .......     ...     ..  ........     ...  .............
.   .     ..    .....     ..  .......     ...     ..  ........     ...  .............
Z  26     AZ    26+26     BZ  2×26+26     ...     ZZ  26×26+26     AAZ  1×26²+1×26+26

Now we can see that ABCD=A×26³+B×26²+C×26¹+D=1×26³+2×26²+3×26¹+4

But how to get the column title from the number? We can't simply use the n%26 method because:

ZZZZ=Z×26³+Z×26²+Z×26¹+Z=26×26³+26×26²+26×26¹+26

We can use (n-1)%26 instead, then we get a number range from 0 to 25.


Code

# Code 1

class Solution:
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        uppercase = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
        res = []
        while n > 0:
            res.append(uppercase[(n - 1) % 26])
            n = (n-1) // 26
        res.reverse()
        return ''.join(res)


# Code 2:

class Solution:
    def convertToTitle(self, num):
        """
        :type n: int
        :rtype: str
        """
        return "" if num == 0 else self.convertToTitle((num - 1) // 26) + chr((num - 1) % 26 + ord('A'))