Skip to content

Instantly share code, notes, and snippets.

@cruxrebels
Created April 28, 2016 20:57
Show Gist options
  • Save cruxrebels/0a6efda9a7d59317b72eac2777ecff93 to your computer and use it in GitHub Desktop.
Save cruxrebels/0a6efda9a7d59317b72eac2777ecff93 to your computer and use it in GitHub Desktop.
Given a column title as appears in an Excel sheet, return its corresponding column number. Example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Tags: InterviewBit Math Problems https://www.interviewbit.com/problems/excel-column-number/
int Solution::titleToNumber(string A) {
auto n = A.length();
int value = 0;
for (auto i=0; i<n; ++i)
{
value += pow(26, i)*(A[n-(i+1)] - 'A' + 1);
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment