数字转换为字母 Excel Sheet Column Title
2019独角兽企业重金招聘Python工程师标准>>>
问题:
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
解决:
① 主要要求就是将10进制转换为26进制。注意26进制的下标是从1开始而不是从0开始。
public class Solution {//0ms
public String convertToTitle(int n) {
StringBuilder sb = new StringBuilder();
while(n > 0){
sb.append((char)(((n - 1)% 26) + ‘A’));//下标从1开始,需要 - 1
n = (n - 1) / 26;
}
return sb.reverse().toString();
}
}
转载于//my.oschina.net/liyurong/blog/1153999
还没有评论,来说两句吧...