C++ iterative clean and short solution. 12ms


  • 0
    F
        int superPow(int a, vector<int>& b) {
            int preBase = a%1337;
            int n = b.size();
            int prev = 1;
            for(int i = n-1; i >= 0; --i) {
                int cur = 1;
                for(int k=0; k<b[i]; ++k) {
                    cur = (cur*preBase) % 1337;
                }
                prev = (cur*prev) % 1337;
                for(int k=b[i]; k<10; ++k) {
                    cur = (cur * preBase) % 1337;
                }
                preBase = cur;
            }
            return prev;
        }

Log in to reply
 

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