Qusetion
Related to question Excel Sheet Column Title
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
MyAnswer
int titleToNumber(char* s) {
int sum = 0;
int i = 0, len = strlen(s);
while(len){
//printf("%c \n",s[len-1]);
sum += (s[--len]-64)*pow(26,i++);
//printf("%d \n",sum);
}
return sum;
}
Others’ Answers
int titleToNumber(char* s)
{
int col=0; // Base case
for (int i=0; s[i]!=0; i++) //iterate over the string
{
col = col*26 + s[i] - 'A' + 1; //(s[i] -'A' + 1) will return the corresponding value of s[i] column
}
return col;
}