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取余范围在0-25.
public class Solution { static HashMap<Integer,Character>map=new HashMap<Integer,Character>(); static { for(int i=0;i<26;i++){ map.put(i, (char)('A'+i)); } } public String convertToTitle(int n) { StringBuffer ret=new StringBuffer(); while(n>0){ int temp=(n-1)%26; //注意减一 ret.append(map.get(temp)); n=(n-1)/26; } return ret.reverse().toString(); } }