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:
Related to 168. Excel Sheet Column Title
Reversion process of 168. Transform a 26 based number to decimal number.
public int titleToNumber(String s) {
if(s==null || s.length()==0) return 0;
int res=0;
int div=1;
for(int i=s.length()-1;i>=0;i--){
res+=div*(s.charAt(i)-'A'+1);
div*=26;
}
return res;
}
No comments:
Post a Comment