首页 > 其他 > 详细

Excel Sheet Column Number--LeetCode

时间:2015-04-12 12:04:45      阅读:191      评论:0      收藏:0      [点我收藏+]

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 


思路:进制之间的转化,不过这里需要考虑的是这里没有0这个数,不过可以模26,然后对于结果是0的看做26.

string convertToTitle(int n) 
{
    string result;
    int num = n;
    int temp;
    while(num)
    {
       temp = num%26;
       temp = temp ==0? 26:temp;
       result += (temp+'A'-1);
       num = (num-temp)/26;       
    }    
    reverse(result.begin(),result.end());
    return result;    
}


Excel Sheet Column Number--LeetCode

原文:http://blog.csdn.net/yusiguyuan/article/details/45008373

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!