168. Excel Sheet Column Title

原题链接:https://leetcode.com/problems/excel-sheet-column-title/description/
这道题目虽然是easy级别的,初看也以为很简单,但是我并未能写出一个完整的实现来。最后,还是抄袭别人的答案:

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    public static void main(String[] args) {
        Solution s = new Solution();
        for (int i = 26; i < 55; i++) {
            System.out.println(s.convertToTitle(i));
        }
    }

    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            n--;
            sb.append(convertToTitleHelper(n % 26));
            n /= 26;
        }
        return sb.reverse().toString();
    }

    private char convertToTitleHelper(int n) {
        return (char) ('A' + n);
    }
}
posted @ 2018-03-21 18:23  optor  阅读(112)  评论(0编辑  收藏  举报