168. Excel Sheet Column Title

Description

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

Constraints

Approach

Examples

Input: 1

Output: "A"

Solutions

/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public String convertToTitle(int n) {
        if(n == 0) return "";

        char[] alphabets = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
                            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        StringBuilder result = new StringBuilder();

        while(n > 0) {
            n--;
            result.append(alphabets[n%26]);
            n /= 26;
        }

        return result.reverse().toString();
    }
}

Follow up

Last updated

Was this helpful?