0%

168. Excel Sheet Column Title

O(n)

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
string convertToTitle(int n) {
string res;
while (n) {
res += 'A' + (--n) % 26;
n /= 26;
}
return string(rbegin(res), rend(res));
}
};

复杂度不是O(n)

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
string convertToTitle(int n) {
string res;
while (n) {
res = char('A' + (--n) % 26) + res; // 这里一定要--n不能n-1,反例26 --> Z不是AZ
n /= 26;
}
return res;
}
};