題目描述:
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)化為進(jìn)制轉(zhuǎn)換問(wèn)題,
LeetCode Excel Sheet Column Number
,電腦資料
《LeetCode Excel Sheet Column Number》(http://www.ishadingyu.com)。思路:和2進(jìn)制轉(zhuǎn)10進(jìn)制有點(diǎn)類(lèi)似,對(duì)于AAA,可以理解為26進(jìn)制的轉(zhuǎn)換:1*26 + 1*26^2 + 1*26^3
實(shí)現(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;}}