码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_num168&171_excel title&number

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

标签:leetcode

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

标签:leetcode

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!