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. string convertToTitle(int n) {
  2. if(n==0) {
  3. return "";
  4. }
  5. string result="";
  6. while(n!=0) {
  7. int mod = n%26;
  8. if(mod == 0) {
  9. mod = 26;
  10. }
  11. char zifu = 'A' + mod - 1;
  12. result = zifu + result ;
  13. n = n/26;
  14. if(mod == 26) n--;
  15. }
  16. return result;
  17. }
posted @ 2014-12-23 19:38  purejade  阅读(111)  评论(0)    收藏  举报