168. Excel Sheet Column Title

2015-05-02  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读