[LeetCode]171. Excel Sheet Column Number(Excel表格列号)

来源:互联网 发布:apche怎么使用php 编辑:程序博客网 时间:2024/06/09 02:18

171. Excel Sheet Column Number

和168题是一个类型的,参考[LeetCode]168. Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.
给出Excel表格中显示的列标题,返回其对应的列号。

For example:

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

代码如下:

#include <iostream>#include <string>using namespace std;class Solution {public:    int titleToNumber(string s) {        int res = 0;        for(int i=0; i<s.size(); i++)            res = res*26 + (s[i]-'A'+1);//A-Z 十进制65-90   A-'A'=0 A-'A'+1=1 B-'A'+1=2        return res;    }};int main(){    Solution a;    string s;    cin >> s;    cout << a.titleToNumber(s) << endl;    return 0;}
0 0
原创粉丝点击