C++实现求Excel表列序号 C++实现LeetCode(171.求Excel表列序号)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++实现求Excel表列序号 C++实现LeetCode(171.求Excel表列序号)

Grandyang   2021-08-02 我要评论
想了解C++实现LeetCode(171.求Excel表列序号)的相关内容吗,Grandyang在本文为您仔细讲解C++实现求Excel表列序号的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++实现求Excel表列序号,C++实现LeetCode求Excel表列序号,下面大家一起来学习吧。

[LeetCode] 171.Excel Sheet Column Number 求Excel表列序号

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 

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这题实际上相当于一种二十六进制转十进制的问题,并不难,只要一位一位的转换即可。代码如下:

class Solution {
public:
    int titleToNumber(string s) {
        int n = s.size();
        int res = 0;
        int tmp = 1;
        for (int i = n; i >= 1; --i) {
            res += (s[i - 1] - 'A' + 1) * tmp; 
            tmp *= 26;
        }
        return res;
    }
};

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们