leetcode Excel Sheet Column Title

来源:互联网 发布:淘宝每天可以花5000 编辑:程序博客网 时间:2024/06/14 08:58

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 


关键在于n是先取模,再在赋值ASCII码的时候减去1,还是先减去1,再取模。纠结好久敲打

class Solution {public:    string convertToTitle(int n) {        string re = "";                    while (n > 0) {            re += --n % 26 + 'A';             n /= 26;         }        reverse(re.begin(), re.end());                return re;    }};


0 0
原创粉丝点击