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
Solution:
This is an easy question. Just like Hex converting to Decimal.
Note: If we use a pointer i reversely go through the array (convert from the given string), the order of 26 should be the "distance" between current pointed element and the last element.
public class Solution
{
public int titleToNumber(String s)
{
int result = 0;
char[] strArray = s.toCharArray();
for(int i = s.length() - 1; i >= 0; i--)
{
result += (Math.pow(26, s.length() - 1 - i) * ((int)strArray[i] - 'A' + 1));
}
return result;
}
}