Java solution beats 100%


  • 0
    H
    public class Solution {
        public int superPow(int a, int[] b) {
            if(a == 1)
                return 1;
            if(a == 1337)
                return 0;
            a = a % 1337;
            int[] modlist = new int[1337];
            int start = a % 1337;
            int idx = start, value = (idx * a) % 1337;
            int period = 0;
            while(modlist[idx] != value){
                modlist[idx] = value;
                idx = value;
                value = (value * a) % 1337;
                ++period;
            }
            int r = 0;
            for(int i : b){
                r = (10 * r + i) % period;
            }
            if(r == 1)
                return start;
            int c = 1;
            idx = start;
            while(c < r){
                value = modlist[idx];
                idx = value;
                ++c;
            }
            return value;
        }
    }
    

Log in to reply
 

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