168. Excel Sheet Column Title

来源:互联网 发布:教育软件平台公司 编辑:程序博客网 时间:2024/05/18 03: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进制数和10进制数的互相转换

代码如下(已通过leetcode)

public class Solution {
   public String convertToTitle(int n) {
    String ans="";
    String temp;
    boolean isfirst=true;
    while(n>0) {
    int dig1=(n-1)%26;
    temp=(char)(dig1+'A')+"";
    ans=temp+ans;
    n=(n-1)/26;
    }
    return ans;
   }
}

0 0
原创粉丝点击