您的位置:首页 > 其它

Excel Sheet Column Title

2015-07-01 22:19 253 查看
https://leetcode.com/problems/excel-sheet-column-title/

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

解题思路:

Excel Sheet Column Number 的follow-up,像上一题提到的。26进制实际上是从0-25,而本题的数字是从1-26。所以计算的时候,需要将数字减一。

public class Solution {
public String convertToTitle(int n) {
String res = "";
while(n > 0) {
res = (char)('A' + (n - 1) % 26) + res;
n = (n - 1) / 26;
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: