LeetCode之Excel Sheet Column Number
发布日期:2021-06-29 14:08:11 浏览次数:2 分类:技术文章

本文共 999 字,大约阅读时间需要 3 分钟。

1、题目

 

Related to question

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

Credits:

Special thanks to for adding this problem and creating all test cases.

to see which companies asked this question.

 

 

 

 

 

2、分析

 

A -> 1*26^0AA -> 1*26^1 + 1*26^0AAA -> 1*26^2 + 1*26^1 + 1*26^0

 

 

 

 

 

 

3、代码实现

 

public class Solution {    public int titleToNumber(String value) {       if (value == null || value.length() == 0) 			return 0;		int length = value.length();		//A 65		char[] chars = value.toCharArray();		int result = 0;		int pow = 0;		//please here is i >= 0 not is i > 0		for (int i = length - 1; i >= 0; i--) {			int tmp = chars[i] - 'A' + 1;			int temp1 = (int)Math.pow(26, pow);			pow++;			result += tmp * temp1;		}		return result;     }}

 

 

 

 

 

 

 

4、总结

注意每次写

 

for(int i = length - 1; i > 0 --i)

 

 

 

的时候要注意不是这样写的,需要写成这样

 

for(int i = length - 1; i >= 0 --i)

不要忘记有=号,切记,以后不要换这样的错误。

 

还有求^的函数要知道,不要忘记

 

Math.pow(26, pow)

 

 

 

 

 

 

 

 

 

转载地址:https://chenyu.blog.csdn.net/article/details/68950225 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode之Number Complement
下一篇:LeetCode之Move Zeroes

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月05日 17时16分55秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

移植 RT-Thread Nano 到 RISC-V 2019-04-29
软件包应用分享|基于RT-Thread的百度语音识别(二) 2019-04-29
在 RT-Thread Nano 上添加控制台与 FinSH 2019-04-29
一站式开发工具:RT-Thread Studio 正式发布 2019-04-29
留言有礼|谢谢你悄悄点了小星星,让我们跃居GitHub RTOS Star榜第一 2019-04-29
功能更新!C 函数也能在 MicroPython 中被调用啦 2019-04-29
东软载波携ES32+RT-Thread走进海尔集团 2019-04-29
今晚8点直播预告:RT-Thread Studio等相关主题答疑 2019-04-29
Linux内核在中国大发展的黄金十年-写于中国Linux存储、内存管理和文件系统峰会十周年之际... 2019-04-29
物联网 20 年简史大揭秘! 2019-04-29
开源项目|RT-Thread 软件包应用作品:水墨屏桌面台历 2019-04-29
珠联璧合!基于i.MX RT和RT-Thread的物联网云接入方案 2019-04-29
基于RTT-MicroPython制作自带BGM的新型肺炎晴雨表 2019-04-29
Arm宣布推出Cortex-M55核心和Ethos-U55 microNPU,瞄准低功耗Edge AI 2019-04-29
开源项目|RT-Thread 软件包应用作品:小闹钟 2019-04-29
在 RT-Thread Studio 上使用 RT-Thread Nano 2019-04-29
开源项目|软件包应用作品:通用物联网系统平台 2019-04-29
【经验分享】RT-Thread UART设备驱动框架初体验(中断方式接收带\r\n的数据) 2019-04-29
单片机里面的CPU使用率是什么鬼? 2019-04-29
推荐一个优质Linux技术公众号-作者都是一线Linux代码贡献者们哦 2019-04-29