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 


1->A 27->AA

C++(3ms):
 1 class Solution {
 2 public:
 3     string convertToTitle(int n) {
 4         string res = "" ;
 5         char tmp ;
 6         while(n){
 7             n -= 1 ;
 8             tmp = 'A' + n%26 ;
 9             res = tmp + res ;
10             n /= 26 ;
11         }
12         return res ;
13     }
14 };

 

 
posted @ 2018-03-21 15:29  __Meng  阅读(115)  评论(0编辑  收藏  举报