LeetCode 171. Excel Sheet Column Number 题解 —— Java

来源:互联网 发布:武汉最新网络约车新规 编辑:程序博客网 时间:2024/06/13 12:30

题目链接:https://leetcode.com/problems/excel-sheet-column-number/#/description

题目要求:实质上是26机制的数转化为十进制。

思路:举例来说,BAA=((B*26 + A) * 26 ) +A


Java代码如下:

public class Solution {// 26进制的计算    public int titleToNumber(String s) {        if(s.length() == 0){        return 0;        }        int result = s.charAt(0) - 'A' + 1;    for(int i=1; i<s.length(); i++){    result = result * 26 + (s.charAt(i) - 'A' + 1) ;    }    return result;    }}


0 0
原创粉丝点击