Why the similar solution of Java can pass, but this python solution got a Time_Exceed error? They are both O(n^2) time complexity.


  • 0
    V

    class Solution(object):
    def subarraySum(self, nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: int
    """
    n=len(nums)
    count = 0
    for i in range(n):
    s=0
    for j in range(i, n):
    s += nums[j]
    if s == k:
    count += 1
    return count


  • 0

    It's obvious that python code runs slower than java.


Log in to reply
 

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