题意:
解题思路:
思路:
(0~25转ASCII则在每位+'A'),PHP则加上65,因为chr(65) == A
0, 1, 2, 3.......25
+ 'A'
A, B, C, D.......Z
-------------------------
问题:1 % 26 => 1 ??跟题目的0不一致,1映射到B,但题目实际要求映射到了A;
解决办法:对整数 -1(减一),即 0 % 26 =》0,0映射到了A;
PHP代码实现:
class Solution {
/**
* @param Integer $n
* @return String
*/
function convertToTitle($n) {
if ($n <= 0) return "";
$s = "";
while ($n > 0) {
$n--;
$s = chr(fmod($n, 26) + 65). $s;
$n = floor($n / 26);
}
return $s;
}
}
go代码实现:
func convertToTitle(n int) string {
s := ""
for n != 0 {
n--
s = string(n % 26 + 'A') + s
n /= 26
}
return s
}