Tuesday, April 14, 2015

171. Excel Sheet Column Number Leetcode Java

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:
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