leetCode(48):Excel Sheet Column Number And Excel Sheet Column Title 分类: leetCode 2015-07-24 08:51 146人阅读 评论(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;
    }
};




原文地址:https://www.cnblogs.com/zclzqbx/p/4687048.html