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
此题乍一看,很简单,但程序一次通过却不容易,有一些容易忽略的点。(结合程序来说) 如对Z的处理,用map保存时,它对26的余数应该是0。 而且还要注意对n进行更新时,n = n/26是错误的。也是因为当n = 26时 就不需要进入下轮循环了。
public String convertToTitle(int n) { //java Map<Integer,Character> map = new HashMap<Integer,Character>(); for(int i = 1; i<= 26; i++){ Integer key = i%26; Character value = (char)(64+i); map.put(key,value); } String result = ""; while(n!=0){ int tmp = n%26; result = map.get(tmp) + result; n = (n-1)/26; } return result; }
[leetcode]Excel Sheet Column Title
原文地址:http://blog.csdn.net/chenlei0630/article/details/42211819