Java easy and clear Solution


  • 0
    A
    public class Solution {
        public int superPow(int a, int[] b) {
            if (a == 0) return 0;
            if (b == null || b.length ==0) return 1;
            a = a % 1337;
            int ans = pow(a, b[b.length - 1]);
            int tmp = a;
            for (int i = b.length - 2; i >= 0; i--) {
                tmp = pow(tmp, 10);
                if (b[i] != 0)
                    ans = ans * pow(tmp, b[i]) % 1337;
            }
    
            return ans;
        }
    
        public int pow(int a, int b) {
            int ans = 1;
            if (b == 0) return 1;
            for (int i = 0; i < b; i++) {
                ans = (ans * a) % 1337;
            }
            return ans;
        }
    
    }

Log in to reply
 

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