My C# solution, beat 90%


  • 0
    L
    public int SuperPow2(int a, int[] b) {
        if (b != null && b.Length > 0) {
            if (b.Length == 1 && b[0] == 0)
                return 1;
    
            if (a == 0)
                return 0;
    
            int[] liaQuotient = new int[10];
            Dictionary<int, int> ldic = new Dictionary<int, int>();
    
            liaQuotient[0] = 1;
            liaQuotient[1] = a % 1337;
            for (int i = 1; i < 10; ++i) {
                liaQuotient[i] = (liaQuotient[i - 1] * liaQuotient[1]) % 1337;
            }
    
            int lintResult = 1;
            for (int i = 0; i < b.Length; ++i) {
                int mulValue = 1;
    
                if (ldic.ContainsKey(lintResult)) {
                    mulValue = ldic[lintResult];
                } else {
                    for (int j = 0; j < 10; ++j) {
                        mulValue = (mulValue * lintResult) % 1337;
                    }
                    ldic.Add(lintResult, mulValue);
                }
                lintResult = (mulValue * liaQuotient[b[i]]) % 1337;
            }
    
            return lintResult; 
        } else {
            return 0;
        }
    }
    

Log in to reply
 

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