# LeetCode-Alg-168-Excel Sheet Column Title

## LeetCode-Alg-168-Excel Sheet Column Title

Posted by Jae on May 31, 2019

### 1、题目

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


Example 1:

Input: 1
Output: "A"
Example 2:

Input: 28
Output: "AB"
Example 3:

Input: 701
Output: "ZY"


### 2、思路

1 2 3 4 5 6 ... 25 26
A B C D E F ... Y  Z
27 28 29 30 ... 51 52
AA AB AC AD ... AY AZ
53 53 55 56 ... 77 78
BA BB BC BD ... BY BZ
...


2）如果商的值大于26则需要再次对商进行(1)的操作,否则找到商对应A-Z的那个字母;

3) 直到商的范围在[1, 26]之间(余数范围一定在[1, 26])时输出结果。

### 3、实现

public class Solution {

public String convertToTitle(int n) {
StringBuilder sb = new StringBuilder();
return convert(n/26, n%26, new StringBuilder());
}

public String convert(int n, int rem, StringBuilder sb){
if (rem == 0){
n -= 1;
rem = 26;
}
sb.append((char)(rem + 64));
if (n <= 26){
if (n > 0){
sb.append((char)(n + 64));
}
return sb.reverse().toString();
}

return convert(n/26, n%26, sb);
}
}