Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, and return its corresponding column number.
For example:
1, B, 2 C-3 ... 27 AA, Z
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Solution: Equivalent to 26 binary to 10 binary.
classSolution { Public: intTitletonumber (strings) {intn = s.size (), res =0; for(inti =0; I < n; ++i) Res+ = (ToUpper (s[i))-'A'+1) * (int) Pow ( -, N-i-1); returnRes; }};
Improvements that do not use the POW function:
classSolution { Public: intTitletonumber (strings) {intn = s.size (), res =0, i =0; for(; i < n-1; ++i) {//Note that you cannot loop to n-1 to prevent overflowRes + = (ToUpper (s[i))-'A'+1); Res*= -; } returnRes + (ToUpper (s[i))-'A'+1); }};
[Leetcode]61. Excel Sheet column number for Excel columns