Seawolf 发表于 2019-9-14 14:23:01

leetcode 171. Excel Sheet Column Number

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
    ...
Example 1:

Input: "A"
Output: 1
Example 2:

Input: "AB"
Output: 28
Example 3:

Input: "ZY"
Output: 701

class Solution {
    public int titleToNumber(String s) {
      if(s.length() == 0) return 0;
      
      if(s.length()>1)
            return (s.charAt(0) - 'A'+1)*((int)Math.pow(26,s.length() - 1)) + titleToNumber(s.substring(1));
      else return (s.charAt(0) - 'A'+1)*((int)Math.pow(26,s.length() - 1));
    }
}
页: [1]
查看完整版本: leetcode 171. Excel Sheet Column Number