Excel Sheet Column Title (Easy)

Description

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

1
2
3
4
5
6
7
8
9
For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

Analysis

简单题

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//C++
class Solution {
public:
string convertToTitle(int n) {
string ans;
while(n){
if(n%26==0){
ans+='Z';
n/=26;n--;
continue;
}
ans+=n%26-1+'A';
n/=26;
}
reverse(ans.begin(),ans.end());
return ans;
}
};