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

 

class Solution {
    public String convertToTitle(int n) {
        String str = new String("ZABCDEFGHIJKLMNOPQRSTUVWXY");
        StringBuilder res = new StringBuilder();
        while(n>0){
            res.insert(0,String.valueOf(str.charAt(n%26)));
            if(n %26 == 0) n--;
            n /= 26;
        }
        return res.toString();
    }
}

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「168. Excel Sheet Column Title」

Praise 172
0 0 172

Further reading

Post a reply

Log in can only be commented on later
Share this page
Back to top