168. 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 

链接: http://leetcode.com/problems/excel-sheet-column-title/

2/20/2017, Java

看别人的解法,从最低位开始算

一开始想从最高位开始算,然而高位是在之前低位运算基础上来的。

 1 public class Solution {
 2     public String convertToTitle(int n) {
 3         StringBuilder s = new StringBuilder();
 4         while (n > 0) {
 5             n--;
 6             s.append((char)(n % 26 + 'A'));
 7             n /= 26;
 8         }
 9         s.reverse();
10         return s.toString();
11     }
12 }

 

posted @ 2017-02-21 00:54  panini  阅读(124)  评论(0编辑  收藏  举报