和大神们学习每天一题(leetcode)-Excel Sheet Column Title

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,1,16384

功能测试用例:26, 2843

代码如下:

class Solution 
{
public:
	string convertToTitle(int n) 
	{
		string strResult , strMiddle;
		if (n == 0)//输入为0直接返回空字符串
			return strResult;
		int nRemain = n;
		while (nRemain > 0)
		{
			strMiddle = (nRemain - 1) % 26 + 'A';
			strResult = strMiddle + strResult;
			nRemain = (nRemain - 1) / 26;
		}
		return strResult;
	}
};





你可能感兴趣的:(Leetcode,leetcode,windows,编程)