Formula explained. Fast Python solution


  • 0
    C

    At first it was confusing but I later figure out what the question is asking
    for example if the input is 'CDA', and the values are 3,4,1
    the index of each char in the string in reverse order plus one is 2, 1, 0

    the formula is

    result = value(char) * 26^(order(char))

    so in this case.

    result = 3 * (26^2) + 4 * (26^1) + 1 * 26^0 = 2133

    This makes sense to me and hope my explanation is helpful

    class Solution(object):
        def titleToNumber(self, s):
            res = 0
            for i in range(len(s)):
                coef = ord(s[-1-i])-64
                value = 26**i
                res += (coef*value)
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.