Algorithm


Problem Name: 171. Excel Sheet Column Number

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.

For example:

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

 

Example 1:

Input: columnTitle = "A"
Output: 1

Example 2:

Input: columnTitle = "AB"
Output: 28

Example 3:

Input: columnTitle = "ZY"
Output: 701

 

Constraints:

  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"]

Code Examples

#1 Code Example with C Programming

Code - C Programming


int titleToNumber(char* s) {
    int k = 0;
    while (s && *s) {
        k = k * 26 + (*s) - 'A' + 1;
        s ++;
`   }
    return k;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnTitle = "A"

Output

x
+
cmd
1

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int titleToNumber(string s) {
        int res = 0;
        for(auto c: s){
            res *= 26;
            res += c - 'A' + 1;
        }
        return res;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnTitle = "A"

Output

x
+
cmd
1

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int titleToNumber(String columnTitle) {
    int numericValue = 0;
    int multiplier = 1;
    for (int i = columnTitle.length() - 1; i >= 0; i--) {
      int currCharNum = columnTitle.charAt(i) - 'A' + 1;
      numericValue += multiplier * currCharNum;
      multiplier *= 26;
    }
    return numericValue; 
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnTitle = "AB"

Output

x
+
cmd
28

#4 Code Example with Javascript Programming

Code - Javascript Programming


const titleToNumber = function(s) {
  const arr = s.split("");
  const len = arr.length;
  const uac = "A".charCodeAt(0);
  return arr.reduce((ac, el, idx, arr) => {
    return ac + Math.pow(26, len - idx - 1) * (`${el}`.charCodeAt(0) - uac + 1);
  }, 0);
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnTitle = "AB"

Output

x
+
cmd
28

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        return sum([(ord(char)-64)*(26**i) for i,char in enumerate(s[::-1])])
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnTitle = "ZY"

Output

x
+
cmd
701

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0171_ExcelSheetColumnNumber
    {
        public int TitleToNumber(string s)
        {
            var value = 0;
            foreach (var ch in s)
            {
                value *= 26;
                value += ch - 'A' + 1;
            }

            return value;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
columnTitle = "ZY"

Output

x
+
cmd
701
Advertisements

Demonstration


Previous
#169 Leetcode Majority Element Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#172 Leetcode Factorial Trailing Zeroes Solution in C, C++, Java, JavaScript, Python, C# Leetcode