LEETCODE 168

来源:互联网 发布:zuul nginx 编辑:程序博客网 时间:2024/04/27 19:04

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 

思路,相当于10进制进位,比如个位到Z以后,向上一位进一个,变成AA。

class Solution {public:    string convertToTitle(int n) {  if (0 == n)return NULL;string temp = ""; while(n){char s = (n - 1)%26 + 'A';temp = s + temp;n =(n-1)/26;}return temp;    }};


0 0
原创粉丝点击