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

    class Solution(object):
    def subarraySum(self, nums, k):
    :type nums: List[int]
    :type k: int
    :rtype: int
    count = 0
    for i in range(n):
    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.