A 14ms iterative c++ solution O(b.size())


  • 0
    R

    A solution adapted form Wiki

    class Solution {
    public:
        int superPow(int a, vector<int>& b) {
            int n = b.size();
            int result = 1;
            for(int i = n-1; i >=0; i--) {
                result = ((result * modPower(a, b[i])) % 1337);
                a = modPower(a, 10);
            }
            return result;
        }
    private:
        int modPower(int a, int e) {
            int result = 1;    
            a = a%1337;
       
            while (e > 0) {
                if (e & 1) {
                    result = ((result * a) % 1337);
                }
                e >>= 1;
                a = ((a * a) % 1337);
            }
            return result;
        }
    };
    

Log in to reply
 

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