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 
cpp:
class Solution {
public:
    string convertToTitle(int n) {
        string result;

        int a;
        while (n > 0) {
            a = (n - 1) % 26;
            n = (n - 1) / 26;
            result.push_back(a + 'A');
        }

        reverse(result.begin(), result.end());
        return result;
    }
};

python:

class Solution(object):
    def convertToTitle(self, n):
        s = []
        while n > 0:
            a = (n - 1) % 26
            s.append(chr(a+65))
            n = int((n - 1) / 26)
        return ''.join(s[::-1])

 

posted @ 2016-02-27 11:15  wxquare  阅读(196)  评论(0编辑  收藏  举报