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
实现代码:
/***************************************************************************** * @COPYRIGHT NOTICE * @Copyright (c) 2015, 楚兴 * @All rights reserved * @Version : 1.0 * @Author : 楚兴 * @Date : 2015/2/6 14:29 * @Status : Accepted * @Runtime : 15 ms *****************************************************************************/ #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution { public: int titleToNumber(string s) { int column = 0; for (int i = 0; i < s.size(); i++) { if (s[i] >= 'A' && s[i] <= 'Z') { column = column * 26 + s[i] - 'A' + 1; } } return column; } }; int main() { Solution s; int a = s.titleToNumber("AA"); cout<<a<<endl; system("pause"); }
时间: 2024-08-01 22:49:50