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
Source
public class Solution {
public int titleToNumber(String s) {
int i,num = 0;
i = 0;
while( i < s.length()){
if(s.charAt(i) != 'Z'){
num = num * 26 + s.charAt(i) - 65 + 1;
}
else num = num * 26 + 26;
i++;
}
return num;
}
}
Test
public static void main(String[] args){
System.out.println(new Solution().titleToNumber("AA"));
}