Problem

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

Example

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 

Solution

public class Solution {
    /**
     * @param s: a string
     * @return: return a integer
     */
    public int titleToNumber(String s) {
        // write your code here
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i > 0) res *= 26;
            char ch = s.charAt(i);
            res += (ch-'A'+1);
        }
        return res;
    }
}

linspiration
161 声望53 粉丝