Java for LeetCode 168 Excel Sheet Column Title

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

解题思路:

JAVA实现如下:

	static public String convertToTitle(int n) {
		StringBuilder sb = new StringBuilder();
		while (n > 0) {
			int temp='A'+n%26-1;
			if(temp<'A')
				temp+=26;
			sb.insert(0,(char)temp);
			n/=26;
			if(temp=='Z')
				n--;
		}
		return sb.toString();
	}

 

posted @ 2015-06-05 23:53  TonyLuis  阅读(173)  评论(0编辑  收藏  举报