天天看点

LEETCODE 168

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       

思路,相当于10进制进位,比如个位到Z以后,向上一位进一个,变成AA。

class Solution {
public:
    string convertToTitle(int n) {
  if (0 == n)
			return NULL;
		string temp = ""; 
		while(n)
		{
			char s = (n - 1)%26 + 'A';
			temp = s + temp;
			n =(n-1)/26;
		}

		return temp;
    }
};