leetcode 168. 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 

 

 

并不难的一道题吧,感觉有点类似26进制?

但是刚开始有些出错,就是对应规则那边吧,需要再理解一下。

 

class Solution {
public:
    string convertToTitle(int n) {
        string ret="";
        int c=0;
        while(n>0){
            n--;         //对应的规则是1---A,计算的时候是0----A
            c=n%26;
            char tmp;
            tmp='A'+c;
            ret=tmp+ret;
            n/=26;}
        return ret;
    }
};

 

posted @ 2016-01-04 16:19  0giant  阅读(141)  评论(0编辑  收藏  举报