我是靠谱客的博主 舒适仙人掌,这篇文章主要介绍C++实现LeetCode(171.求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;
    }
};

到此这篇关于C++实现LeetCode(171.求Excel表列序号)的文章就介绍到这了,更多相关C++实现求Excel表列序号内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是舒适仙人掌最近收集整理的关于C++实现LeetCode(171.求Excel表列序号)的全部内容,更多相关C++实现LeetCode(171内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(104)

评论列表共有 0 条评论

立即
投稿
返回
顶部