转化成26进制的问题,唯一的区别就是不是0 base而是1 base了,随之而来的问题就是比如说52就会表示成AZ,A0和Z都表示26,但是因为我们不存在0,所以表示方法还是唯一的。大体的方法还是相同的,我们只需要确保当前数n恰好为26的倍数的时候不要全部带到下一位,比如52的情况,我们确定第一位是Z,带到下一位的时候就不应该是52/2 = 2,而应该是1。我们只需要简单的在开始对n - 1即可,代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string convertToTitle(int n) { | |
int base = 26; | |
string res; | |
while(n) | |
{ | |
--n; | |
char digit = (n % base) + 'A'; | |
res = digit + res; | |
n /= base; | |
} | |
return res; | |
} | |
}; |
No comments:
Post a Comment