Leetcode 171.Excel Sheet Column Number

Related to question
Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.

思路:26进制转为10进制的表示,注意判断字符串中的每个字符是否都合法。

public int titleToNumber(String s) {
    if (s == null || s.length() == 0) {
        return 0;
    }

    int res = 0;
    for (int i = 0; i < s.length(); i++) {
        int num = s.charAt(i) - 'A' + 1;
        if (num < 1 || num > 26) {
            return 0;
        }
        res = res * 26 + num;
    }

    return res;
}
    原文作者:ShutLove
    原文地址: https://www.jianshu.com/p/74cf79321735
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞