您的位置:首页 > 其它

171. Excel Sheet Column Number

2016-05-14 22:05 295 查看

171. Excel Sheet Column Number

Description:

Given a column title as appear in an Excel sheet, return its corresponding column number.

Example:

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


Link:

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

Analysis:

1.要弄清题目的意思,打开个EXCEL表格看一下就了然了。

2.我的理解是这只是进制之间的一种转换关系,AA 等这些数给人的感觉类似于26进制数。针对于本题就是26进制转10进制了。例如:A=1, AA=1*26+1, BA=2*26+1 。详见代码。

Source Code(C++):

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class Solution {
public:
int titleToNumber(string s) {
int num=0;
for (int i=s.length()-1; i>=0; i--) {
int times = pow((float)26,(int)s.length()-1-i);
num += (s.at(i)-64)*times;
}
return num;
}
};

int main() {
Solution sol;
cout << sol.titleToNumber("AA");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: