LeetCode|Excel Sheet Column Number-java

来源:互联网 发布:sqlite for mac 编辑:程序博客网 时间:2024/05/23 01:34

题目:

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 

思路:

将26进制转换为10进制

public class Solution {    public static int titleToNumber(String s) {        int result = 0;        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            result = result * 26 + (c - 'A' + 1);        }        return result;    }}


0 0
原创粉丝点击