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 class Solution {
 2 public:
 3     string convertToTitle(int n) {
 4         if(n<1) return "";
 5         else
 6         {
 7             string ret="";
 8             while(n)
 9             {
10                 n--;
11                 char c=n%26+'A';
12                 ret=c+ret;
13                 n/=26;
14             }
15               return ret;
16         }
17     }
18 };

 

posted on 2015-05-25 11:25  黄瓜小肥皂  阅读(183)  评论(0编辑  收藏  举报