leetcode

Excel Sheet Column Number

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

We can see the pattern there is a cycle for every 26 numbers, so the formula is result = result *26+ currentSymbol.

public int titleToNumber(String s) {        
        int i=0;
        int result = 0;
        int temp = 0;
        while (i<s.length()) {
            temp = s.charAt(i)-'A'+1;
            result = result * 26 + temp;
            i++;
        }
        return result;
    }