168. Excel Sheet Column Title

Total Accepted: 51862
Total Submissions: 249733
Difficulty: Easy

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

Java:

1
2
3
4
5
6
7
8
9
10
public class Solution {
    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        while(n!=0){    
            sb.insert(0,(char)('A' + --n % 26));
            n/=26;
        }
        return sb.toString();
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。