LeetCode_168. Excel Sheet Column Title

1163 단어

168. Excel Sheet Column Title
Easy
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 
    ...

Example 1:
Input: 1
Output: "A"

Example 2:
Input: 28
Output: "AB"

Example 3:
Input: 701
Output: "ZY"

 
package leetcode.easy;

public class ExcelSheetColumnTitle {
	@org.junit.Test
	public void test() {
		System.out.println(convertToTitle(1));
		System.out.println(convertToTitle(28));
		System.out.println(convertToTitle(701));
	}

	public String convertToTitle(int n) {
		StringBuffer buffer = new StringBuffer();
		while (n > 0) {
			n--;
			buffer.append((char) ('A' + n % 26));
			n /= 26;
		}
		return buffer.reverse().toString();
	}
}

좋은 웹페이지 즐겨찾기