leetcode#168. Excel Sheet Column Title

来源:互联网 发布:汉宁窗 c语言 编辑:程序博客网 时间:2024/05/19 14:51

Description

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 

Code

class Solution:    def convertToTitle(self, n):        """        :type n: int        :rtype: str        """        if n == 0:            return ""        else:            return self.convertToTitle(int((n - 1) / 26)) + chr((n - 1) % 26 + ord('A'))

Conclusion

变型的进制问题。这里遇到个python3的坑,整数相除,得到的是浮点数了,不再取整了。

原创粉丝点击