My 8ms C++ Solution


  • 0
    J
    class Solution {
    public:
        int titleToNumber(string s) {
            int digit = s.length();
            int num = 0;
            for(int i = 0; i < digit-1; i++)
            {
                num += s[i] - 'A' + 1;
                num *= 26;
            }   
            return (num + s[digit-1] - 'A' + 1);
        }
    };
    

    I also tried using pow() function, it makes computation slower. The solution above faster than using pow() function, but need to be careful to avoid overflow.


  • 0
    D

    why not put "num + s[digit-1] -'A' + 1 into for loop?


  • 0
    J

    Interesting...the similar method i used cost me 16ms.


Log in to reply
 

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