Lettcode_168_Excel Sheet Column Title

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42554641


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 

思路:

(1)题意为给定任意整数,求出其对应在Excel中列所对应的字符串。

(2)这道题实质是考察“二十六进制”的运用。由于题比较简单,这里就不啰嗦了,详见下方代码。

(3)希望本文对你有所帮助。


算法代码实现如下:

public static String convertToTitle(int num) {
	if (num < 1) {
		return "";
	} else {
		String temp = "";
		StringBuffer buffer = new StringBuffer();
		while (num > 0) {
			num--;
			char c = (char) (num % 26 + 'A');
			temp += c;
			num /= 26;
		}
		for (int i = temp.length() - 1; i >= 0; i--) {
			buffer.append(temp.charAt(i));
		}
		return buffer.toString();
	}
}



你可能感兴趣的:(java,LeetCode,算法,Excel)