码迷,mamicode.com
首页 > 其他好文 > 详细

Microsoft-Excel Sheet Column Number

时间:2018-04-29 12:03:55      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:soft   进制   span   app   microsoft   ==   its   col   就是   

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


本质就是将一个26进制的数,转换成10进制的数

class Solution {
    public int titleToNumber(String s) {
        if(s == null && s.length() == 0){
            return 0;
        }
        int len = s.length();
        int sum = 0;
        for(int i=0; i<len; i++){
            int temp = s.charAt(i) - ‘A‘+ 1;
            sum = sum * 26 + temp;
        }
        return sum;
    }
}

 

Microsoft-Excel Sheet Column Number

标签:soft   进制   span   app   microsoft   ==   its   col   就是   

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/8970255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!