3 lines Python


  • 0

    Two lines if you don't count the import. Using @compton_scatter's trick of simply not doing %k if k is 0, though done by doing %231 instead.

    import numpy as np
    
    class Solution(object):
        def checkSubarraySum(self, nums, k):
            X = np.cumsum([0] + nums) % (k or 2**31)
            return any(x in X[i+2:] for i, x in enumerate(X))
    

    Bad complexity, but does get accepted in around 180 ms.


  • 0
    Z

    probably numpy is also one of the reason to slow the program down I guess.


Log in to reply
 

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