(java)Excel Sheet Column Title

来源:互联网 发布:fake it til make it 编辑:程序博客网 时间:2024/05/18 01:45

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进一位。

代码如下(已通过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