A Simple Mathematical AC C++ Solution


  • 0
    L
    //The idea is simple, just think of the string as a 26 base representation of numbers. Then we have the equation: (Result)_decimal = a_n * 26^(n-1) + a_(n-1) * 26^(n-2) + ... + a0 * 26^0;
        
        class Solution {
        public:
            int titleToNumber(string s) {
                int res = 0;
                for(int i=0; i<s.length(); i++){
                    int exp = s[i] - 'A' + 1;
                    res = exp * pow(26, s.length() - i -1) + res;
                }
                return res;
            }
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.