# TLE for cumulative sum in python

• I wrote my code in python as below, but it always TLE.
After several tests, I changed the code to C++, and it passed.
I know there are O(n) solutions after reading other posts.
Just want to know that if the time limit is too strict for python?

Note:

Python

``````class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
if not nums:
return 0
count = 0
nLen = len(nums)
for i in xrange(nLen):
last = 0
for j in xrange(i, nLen):
last = last + nums[j]
if last == k:
count += 1
return count
``````

C++

``````class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int nLen = nums.size();
int count = 0;
int last = 0;
if (nLen <= 0) {
return 0;
}
for (int i = 0; i < nLen; i++) {
last = 0;
for (int j = i; j < nLen; j++) {
last = last + nums[j];
if (last == k) {
count++;
}
}
}
return count;
}
};
``````

• Looks like the Python time limit is fine but the C++ time limit is too large.

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