首页 > 其他 > 详细

Excel Sheet Column Title

时间:2015-03-26 12:59:00      阅读:181      评论:0      收藏:0      [点我收藏+]

问题来源:https://leetcode.com/problems/excel-sheet-column-title/

/**
 * 
 * <p>
 * ClassName ExcelSheetColumnTitle
 * </p>
 * <p>
 * Description 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
 * </p>
 * 
 * @author TKPad wangx89@126.com
 *         <p>
 *         Date 2015年3月25日 下午10:37:50
 *         </p>
 * @version V1.0.0
 *
 */
public class ExcelSheetColumnTitle {

    public String convertToTitle(int n) {
        String s = "";
        while (n > 0) {
            if (n % 26 == 0) {// 代表一轮循环,那么要将其加上A所对应的整型
                s = (char) (n % 26 + 26 - 1 + 65) + s;
                n = n / 26 - 1;// 要多减去1
            } else {
                s = (char) (n % 26 - 1 + 65) + s;
                n = n / 26;
            }
        }
        return s;
    }

    public static void main(String[] args) {
        // String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(1406);
        // String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(54);
        // String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(28);
        // String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(1406);
        String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(52);
        System.out.println(convertToTitle);
    }
}

Excel Sheet Column Title

原文:http://blog.csdn.net/shijiebei2009/article/details/44648337

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!