LeetCode -- 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
本題可以轉化為進制轉換問題。
思路:和2進制轉10進制有點類似,對於AAA,可以理解為26進制的轉換:1*26 + 1*26^2 + 1*26^3
實現代碼:
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;
}
}