✡ leetcode 168. Excel Sheet Column Title 26进制数字 --------- java

时间:2024-05-10 09:07:02

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

26进制的实现。

没什么难点。就是需要需要注意是 n = (n-1)/26

public class Solution {
public String convertToTitle(int n) {
if (n == 0){
return "";
}
StringBuffer str = new StringBuffer();
while (n != 0){
int pos = (n - 1) % 26;
char ch = (char) ('A' + pos);
str.insert(0, ch);
n = (n - 1) / 26;
}
return str.toString();
}
}