Excel Sheet Column Number - Math - Easy - LeetCode
💻 coding

Excel Sheet Column Number - Math - Easy - LeetCode

1 min read 104 words
1 min read
ShareWhatsAppPost on X
  • 1The article explains how to convert Excel column titles to their corresponding numerical values.
  • 2Examples include 'A' mapping to 1, 'AB' to 28, and 'ZY' to 701.
  • 3The provided solution has a time complexity of O(n) and a space complexity of O(1).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The article explains how to convert Excel column titles to their corresponding numerical values."

Excel Sheet Column Number - Math - Easy - LeetCode

Given a column title as appear 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: "A" Output: 1 Example 2:

Input: "AB" Output: 28 Example 3:

Input: "ZY" Output: 701

Constraints:

1 <= s.length <= 7 s consists only of uppercase English letters. s is between "A" and "FXSHRXW".

public class Solution {
 public int TitleToNumber(string s) {
 int res = 0;
 for(int i=s.Length-1, j=0;i>=0;i--,j++){
 res += (int)Math.Pow(26,j)*(s[i]-64); 
 }
 return res;
 }
}

Time Complexity: O(n)

Space Complexity: O(1)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 1 October 2020 · 1 min read · 104 words

Part of AskGif Blog · coding

You might also like