Leetcode 题目解析之 Excel Sheet Column Number

2022-01-15 12:13:38 浏览数 (1)

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

代码语言:javascript复制
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 

二十六进制

代码语言:javascript复制
    public int titleToNumber(String s) {
        int n = 0;
        int p = 1;
        for (int i = s.length() - 1; i >= 0; i--) {
            n  = (s.charAt(i) - 'A'   1) * p;
            p *= 26;
        }
        return n;
    }

0 人点赞