Excel Sheet Column Number
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
解题思路:
看成是进制转化的问题,这个是26进制转化成10进制。比较简单,代码如下:
class Solution { public: int titleToNumber(string s) { int len=s.length(); if(len==0){ return 0; } int result = 0; for(int i=0; i<len; i++){ result *= 26; result += s[i] - 'A' + 1; } return result; } };
时间: 2024-10-11 11:46:10