LeetCode/168. ExcelSheetColumnTitle

Summary

It was a simple math but I was a bit struggling when solving this. Need to think properly.

Solutions

My Solution

public string ConvertToTitle(int n)
{
    var res = "";

    while (n != 0)
    {
        var reminder = (n - 1) % 26;
        res = (char)(reminder + 65) + res;
        n = (n - 1) / 26;
    }

    return res;
}

Best Solution

// Best solution
public string BestConvertToTitle(int n)
{
    return n == 0 ? "" : BestConvertToTitle(--n / 26) + (char)('A' + (n % 26));
}