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进制,AA则为 1 * 26 + 1 * 1 BB则为 2 * 26 + 2 * 1 C#代码:
public class Solution { public int TitleToNumber(string s) { char[] ch = s.ToCharArray(); int result = 0; int pro = 1;//每多一位,则是可以看成26进制 for(int i = ch.Count() - 1; i >= 0; i--) { if(i == ch.Count() - 1) { result += (int)ch[i] - ‘A‘ + 1; } else { result += pro * ((int)ch[i] - ‘A‘ + 1); } pro *= 26; } return result; } }
时间: 2024-10-19 01:15:22