力扣171. Excel表列序号(进制转换)

力扣171. Excel表列序号(进制转换)

https://leetcode-cn.com/problems/excel-sheet-column-number/

给定一个Excel表格中的列名称,返回其相应的列序号。

例如,

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

输入: "A"
输出: 1
示例 2:

输入: "AB"
输出: 28
示例 3:

输入: "ZY"
输出: 701

思路:26进制数转10进制

#include "stdafx.h"
#include<string>
using namespace std;
class Solution
{
public:
	int titleToNumber(string s)
	{
		int num = s.size() - 1;
		int sum = 0;
		for (int i = num; i >= 0; i--)
		{
			sum = sum + (s[i]-64) * pow(26, num - i);
		}
		return sum;
	}
};

int main()
{
	Solution ss;
	string s = "";
	auto result = ss.titleToNumber(s);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35683407/article/details/105802217