Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 1015 Bytes

171.md

File metadata and controls

38 lines (30 loc) · 1015 Bytes

171. Excel Sheet Column Number

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

Code

class Solution:
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        dic = {c:i for i,c in enumerate(string.ascii_uppercase, 1)}
        res = 0
        n = 1
        for c in s[::-1]:
            res += dic[c] * n
            n *= 26
        return res