Excel Sheet Column Title

easy math string

Problem

Given a positive integer column number, return its Excel column title (A, B, …, Z, AA, AB, …, ZZ, AAA, …).

InputcolumnNumber = 701
Output"ZY"
Excel columns are bijective base 26 — there's no zero digit. Each step: n−=1, append 'A'+n%26, n/=26.

def convert_to_title(n):
    out = []
    while n > 0:
        n -= 1
        out.append(chr(ord('A') + n % 26))
        n //= 26
    return "".join(reversed(out))
function convertToTitle(n) {
  const out = [];
  while (n > 0) {
    n--;
    out.push(String.fromCharCode(65 + (n % 26)));
    n = Math.floor(n / 26);
  }
  return out.reverse().join("");
}
class Solution {
    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            n--;
            sb.append((char) ('A' + n % 26));
            n /= 26;
        }
        return sb.reverse().toString();
    }
}
string convertToTitle(int n) {
    string out;
    while (n > 0) {
        n--;
        out += (char) ('A' + n % 26);
        n /= 26;
    }
    reverse(out.begin(), out.end());
    return out;
}
Time: O(log26 n) Space: O(log26 n)