11
04/2015
[LeetCode] Excel Sheet Column Number
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; } };
二次刷题(2015-08-06)
class Solution { public: int titleToNumber(string s) { int result = 0; int len = s.length(); for(int i=0; i<len; i++){ result *= 26; result += s[i] - 'A' + 1; } return result; } };
0 条评论