Algorithm


Problem Name: 168. Excel Sheet Column Title

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

 

Example 1:

Input: columnNumber = 1
Output: "A"

Example 2:

Input: columnNumber = 28
Output: "AB"

Example 3:

Input: columnNumber = 701
Output: "ZY"

 

Constraints:

  • 1 <= columnNumber <= 231 - 1
 

Code Examples

#1 Code Example with C Programming

Code - C Programming


char* convertToTitle(int n) {
    char *s;
    int sz, l, k;
    int i, j;
    
    sz = 100;
    s = malloc(sz * sizeof(char));
    //assert(s);
    l = 0;
    while (n) {
       if (sz == l) {
         sz *= 2;
         s = realloc(s, sz * sizeof(char));
         //assert(s);
        }
       k = (n - 1) % 26;
       s[l ++] = 'A' + k;
       n = (n - 1) / 26;
    }
    s[l] = 0;
    for (i = 0, j = l - 1; i  <  j; i ++, j --) {
       k = s[i];
       s[i] = s[j];
       s[j] = k;
    }
    return s;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnNumber = 1

Output

x
+
cmd
"A"

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    string convertToTitle(int n) {
        string res;
        char c;
        while(n){
            c = 'A' + (n - 1) % 26;
            res = c + res;
            n = (n - 1) / 26;
        }
        return res;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnNumber = 1

Output

x
+
cmd
"A"

#2 Code Example with C++ Another Programming

Code - C++ Programming


class Solution {
public:
    string convertToTitle(int n) {
        string res = "";
        while(n){
            res.push_back('A' + (n - 1)%26);
            n = (n - 1) / 26;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnNumber = 28

Output

x
+
cmd
"AB"

#4 Code Example with Javascript Programming

Code - Javascript Programming


const convertToTitle = function(n) {
    if (n === 0) {
        return ''
    }
    const res = [];
    const hash = {};
    ('ABCDEFGHIJKLMNOPQRSTUVWXYZ').split('').forEach((el,idx) => {
        hash[idx] = el
    })
 
    while(n > 0) {
        n--;
        res.unshift(hash[n % 26]);
        n = Math.floor(n / 26);
    }

    return res.join('')
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnNumber = 28

Output

x
+
cmd
"AB"

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def convertToTitle(self, n: int) -> str:
        char = str()
        while n > 0:
            if n % 26 == 0:
                char += "Z"
                n = n // 26 - 1
            else:
                char += chr(n % 26 + ord("@"))
                n = n // 26
        return char[::-1]
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnNumber = 701

#6 Code Example with C# Programming

Code - C# Programming


using System.Text;

namespace LeetCode
{
    public class _0168_ExcelSheetColumnTitle
    {
        public string ConvertToTitle(int n)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var sb = new StringBuilder();
            while (n > 0)
            {
                n--;

                var d = n % 26;
                sb.Insert(0, chars[d]);
                n /= 26;
            }

            return sb.ToString();
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnNumber = 701

Output

x
+
cmd
"ZY"
Advertisements

Demonstration


Previous
#167 Leetcode Two Sum II - Input Array Is Sorted Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#169 Leetcode Majority Element Solution in C, C++, Java, JavaScript, Python, C# Leetcode