zoukankan      html  css  js  c++  java
  • 【leetcode】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 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 
    

    解题思路:

    简单的数字和字符串的转换

    Excel Sheet Column Title

    class Solution:
        # @return a string
        def convertToTitle(self, num):
            alpha = [chr(i) for i in range(65,91)]
            res = []
            while num > 0:
                t = num % 26
                print t
                res.append(alpha[t-1])
                num = (num / 26)
                if t == 0:
                    num -= 1
            return ''.join(res[::-1])
    
    s = Solution()
    print s.convertToTitle(52)
    

    Excel Sheet Column Number

    class Solution:
        # @param s, a string
        # @return an integer
        def titleToNumber(self, s):
            res = 0
            l = len(s)
            for i in range(l):
                res *= 26
                res += ord(s[i]) - 64
            return res
    
    s = Solution()
    print s.titleToNumber('Z')
  • 相关阅读:
    POJ 1511
    POJ 1125
    POJ 2240
    POJ 1459
    POJ 1274
    POJ 1789
    POJ 2485,1258
    POJ 1236
    POJ 1273
    Ruby on Rails 观后感
  • 原文地址:https://www.cnblogs.com/MrLJC/p/4192720.html
Copyright © 2011-2022 走看看