168-excle sheet columen title-字符串转换-26进制数

来源:互联网 发布:nginx 缓存查找 编辑:程序博客网 时间:2024/05/11 02:55

【题目】

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进制数的26么有进一位而是Z,需要注意。

【代码】

public class Solution {
    public String convertToTitle(int n) {
    String a[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
   String result="";
while(n>0){
int yu = n%26;
if(yu==0) yu=26;
result=a[yu-1]+result;
n=(n-1)/26;
}
   return result;
}
}

0 0
原创粉丝点击