首页 > 其他 > 详细

leetcode_num168&171_excel title&number

时间:2015-03-10 00:14:13      阅读:250      评论:0      收藏:0      [点我收藏+]

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

class Solution {
public:
    string convertToTitle(int n) {
        string s;
        while(n){
            s.insert(s.begin(),(n-1)%26+'A');//在字符串起始位插入
            n=(n-1)/26;
        }
        return s;
    }
};
由于没有0,所以需要先减1
Given a column title as appear in an Excel sheet, return its corresponding column number.

class Solution {
public:
    int titleToNumber(string s) {
        int rs=0,j,a;
        for(int i=0;i<s.length();i++){
            j=s.length()-i-1;
            a=s[j]-'A'+1;
            rs+=(a*pow(26,i));
        }
        return rs;
    }
};


leetcode_num168&171_excel title&number

原文:http://blog.csdn.net/eliza1130/article/details/44162465

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