天天看點

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;
    }
};