Excel Sheet Column Number

来源:互联网 发布:kmp算法c代码 编辑:程序博客网 时间:2024/05/17 03:37

题目描述

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 
具体可以查看https://oj.leetcode.com/problems/excel-sheet-column-number/

解题思路

解题思路主要是:根据一个字母时共有26个,两位字母26*26中情况,依次类推,找出规律。然后逐位进行计算,下一位=上一位*26+与A的差值。

自己的源代码

package leetcode;public class ExcelSheetColumnNumber {public int titleToNumber(String s) {//处理特殊情况if(s == null || s.length() == 0) return 0;int sum = 0;for(int i = 0; i < s.length(); i++)sum = sum*26 + s.charAt(i) - 'A' + 1;        return sum;    }public static void main(String[] args) {ExcelSheetColumnNumber escn = new ExcelSheetColumnNumber();//String s = "";//String s = null;//String s = "A";String s = "AB";System.out.println(escn.titleToNumber(s));}}


0 0