Love丶FFC's Blog

LintCode_easy:Excel表列序号

2019-09-07 10:40:52
阅读:1318   •   评论:8
标签:,

描述

给定Excel工作表中显示的列名称,返回其对应的列号。

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 

样例

样例1

输入: "AB"
输出: 28

样例2

输入: "AC"
输出: 29

编程语言:Java

解题思想:

1.在草稿纸上算一算即可得到规律。

2.大写字母的ASCII值-对应的列号=64,利用这个公式即可判断每个单一字母对应的列号。

3.将字母看做个位、十位、百位·······,每向前增加一位,对应的列号会大幅增加。

4.以ZBC为例,对应列号=26^2(90-64)+26^1(90-64)+26^0*(90-64)。

时间复杂度:O(n)

IDE代码如下:

  1. package LintCode_Easy;
  2.  
  3. public class _1348 {
  4.  
  5. public static void main(String[] args) {
  6. String s = "ZBC";
  7. int total = 0; //以ZBC为例,列号=26^2*(90-64)+26^1*(90-64)+26^0*(90-64)
  8. for(int i=s.length()-1,j=0;i>=0;i--,j++) //i控制循环次数,j控制幂次
  9. total+=((int)s.charAt(i)-64)*Math.pow(26, j); //大写字母的ASCII值-对应的列号=64
  10. System.out.println(total);
  11. }
  12.  
  13. }

LintCode编辑器代码如下:

  1. public class Solution {
  2. public int titleToNumber(String s) {
  3. int total = 0;
  4. for(int i=s.length()-1,j=0;i>=0;i--,j++)
  5. total+=((int)s.charAt(i)-64)*Math.pow(26, j);
  6. return total;
  7. }
  8. }

评论板

共有 8 条评论

  1. FoorryNox

    lamictal the new viagra The study by Rauschning and Lindgren provided a detailed description of the association between this valve mechanism and flexion and extension movements of the knee 9, 10

  2. Preliaf

    viagra original pill On my right ovary, I had a 13mm and a 15mm

  3. ewSROsBBc

    does viagra help with ejaculation It is based on the principle that most individuals with tinnitus can better tolerate outside noise than they can their own inner head noise

  4. Awjvnk

    buy fenofibrate pills for sale oral fenofibrate 200mg purchase tricor generic

  5. Mjdmew

    how to get ketotifen without a prescription buy geodon 40mg online cheap tofranil 75mg generic

  6. Mzhmvr

    cialis 40mg brand tadalafil 5mg oral sildenafil over counter

  7. Clntcg

    acarbose 50mg us order generic fulvicin 250mg order griseofulvin 250 mg

  8. Ixvfkv

    mintop canada tamsulosin brand ed pills that work

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:492460