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')
  • 相关阅读:
    Mongo 应用查询
    Rocket MQ 问题排查命令
    阿里云部署杂记-节约时间
    linux shell 杂
    垃圾回收算法学习
    Hbase数据读写流程
    TCP 协议相关
    Netty
    ELK
    MiniGUI
  • 原文地址:https://www.cnblogs.com/MrLJC/p/4192720.html
Copyright © 2011-2022 走看看