題目描述:
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
本題可以轉(zhuǎn)化為進制轉(zhuǎn)換問題,
LeetCode Excel Sheet Column Number
,電腦資料
《LeetCode Excel Sheet Column Number》(http://www.oriental01.com)。思路:和2進制轉(zhuǎn)10進制有點類似,對于AAA,可以理解為26進制的轉(zhuǎn)換:1*26 + 1*26^2 + 1*26^3
實現(xiàn)代碼:
public class Solution { public int TitleToNumber(string s) { var sum = 0; for(var i = s.Length - 1; i >= 0; i--){ var d = s.Length - i; var n = Num(s[i]); sum += (int)Math.Pow(26, d-1) * n; } return sum; } private int Num(char c){ return (int)c - 64;}}