美文网首页Leetcode
Leetcode 171. Excel Sheet Column

Leetcode 171. Excel Sheet Column

作者: SnailTyan | 来源:发表于2021-02-04 09:32 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Excel Sheet Column Number

2. Solution

  • Version 1
class Solution:
    def titleToNumber(self, s):
        result = 0
        mapping = {chr(64+i): i for i in range(1, 27)}
        s = list(s)
        s.reverse()

        for index, ch in enumerate(s):
            result = result + mapping[ch] * math.pow(26, index)
        return int(result)
  • Version 2
class Solution:
    def titleToNumber(self, s):
        result = 0
        for ch in s:
            result = result * 26 + ord(ch) - 64
        return result

Reference

  1. https://leetcode.com/problems/excel-sheet-column-number/

相关文章

网友评论

    本文标题:Leetcode 171. Excel Sheet Column

    本文链接:https://www.haomeiwen.com/subject/sgdmtltx.html