LeetCode—Excel表列序号

Excel表列序号(简单)

2020年5月21日

题目来源:力扣
在这里插入图片描述

解题
这道题是Excel表列名称的反向写法,难度低了很多,不用考虑26进制到26自动进位的问题。没什么技巧,暴力破解。

暴力法
字符串转字符数组,从最后开始遍历转成数字,相加得结果

class Solution {
    public static int titleToNumber(String s) {
        char []ss=s.toCharArray();
        int count=0,muti=1;;
        for(int i=ss.length-1;i>=0;i--){
        	int t=ss[i]-'A'+1;
        	count+=t*muti;
        	muti*=26;
        }
        return count;
    }
}

在这里插入图片描述
暴力法plus
不用额外的字符数组,遍历字符串直接转字符,节省空间提升速度。

class Solution {
    public static int titleToNumber(String s) {
        int count=0;
        for(int i=0;i<s.length();i++) {
            int num = s.charAt(i) - 'A' + 1;
            count = count * 26 + num;
        }
        return count;
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值