码迷,mamicode.com
首页 > 其他好文 > 详细

168. Excel Sheet Column Title

时间:2018-03-21 19:50:58      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:void   system   []   column   desc   static   class   convert   .com   

原题链接: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);
    }
}

168. Excel Sheet Column Title

标签:void   system   []   column   desc   static   class   convert   .com   

原文地址:https://www.cnblogs.com/optor/p/8618770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!