leetcode:Excel Sheet Column Number

来源:互联网 发布:中国文化报 知乎 编辑:程序博客网 时间:2024/05/18 03:12


Excel Sheet Column Number

 Total Accepted: 249 Total Submissions: 494My Submissions

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 


public class Solution {    static final int BIT = 26;        public int titleToNumber(String s) {        int ans = 0;        if(s == null || s.length() == 0)return ans;                int value = 1;        for(int i = s.length() - 1; i >= 0;--i){            ans += get(s.charAt(i)) * value;            value *= BIT;        }        return ans;    }        public static int get(char ch){        return ch - 'A' + 1;    }}


0 0