美文网首页
168. Excel Sheet Column Title

168. Excel Sheet Column Title

作者: 金发萌音 | 来源:发表于2015-05-02 23:50 被阅读82次

    题目来自leetcode

    题目

    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 
    

    进制转换的感觉,注意没有零,所以每次循环前num --

    #coding:utf-8
    
    class Solution:
        # @return a string
        def convertToTitle(self, num):
            alphabet = []
            for i in range(97 -32, 123-32):
                temp = chr(i)
                alphabet.append(temp)
    
            rs = ""
            while num > 0 :
                num -= 1
                rs = rs + alphabet[(num) % 26] 
                num /= 26
    
            rs = rs[::-1]
            return rs
    

    相关文章

      网友评论

          本文标题:168. Excel Sheet Column Title

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