Idiot-maker

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

https://leetcode.com/problems/excel-sheet-column-title/

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 

解题思路:

Excel Sheet Column Number 的follow-up,像上一题提到的。26进制实际上是从0-25,而本题的数字是从1-26。所以计算的时候,需要将数字减一。

public class Solution {
    public String convertToTitle(int n) {
        String res = "";
        while(n > 0) {
            res = (char)('A' + (n - 1) % 26) + res;
            n = (n - 1) / 26;
        }
        return res;
    }
}

 

posted on 2015-07-01 22:19  NickyYe  阅读(195)  评论(0编辑  收藏  举报