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

leetCode(48):Excel Sheet Column Number And Excel Sheet Column Title

时间:2015-07-24 09:21:49      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
class Solution {
public:
    int titleToNumber(string s) {
        int result=0;
        for(int i=0;i<s.size();++i)
        {
            result=result*26+(s[i]-64);
        }
        return result;
    }
};


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 


class Solution {
public:
    string convertToTitle(int n) {
        string resultSrt;
    
    	while (n != 0)
    	{//string和容器比较类似
    		int tmp = (n-1) % 26;
		resultSrt.push_back('A'+tmp);//并不单纯的是一个26进制数
    		n = (n-1) / 26;
    	}
    
    	reverse(resultSrt.begin(), resultSrt.end());
    	return resultSrt;
    }
};




版权声明:本文为博主原创文章,未经博主允许不得转载。

leetCode(48):Excel Sheet Column Number And Excel Sheet Column Title

标签:

原文地址:http://blog.csdn.net/walker19900515/article/details/47035347

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