lintcode:子数组之和
程序员文章站
2022-03-05 13:45:42
...
class Solution:
"""
@param nums: A list of integers
@return: A list of integers includes the index of the first number
and the index of the last number
"""
def subarraySum(self, nums):
# write your code here
# 1314ms
n = len(nums)
for st in range(n):
a = 0
for ed in range(st, n):
a += nums[ed]
if a == 0:
return [st, ed]
# hashmap 1183ms
n, a, d = len(nums), 0, {0:-1}
for i in range(n):
a += nums[i]
if a in d:
return [d.get(a) + 1, i]
d[a] = i
上一篇: python模块_Python模块
下一篇: lintcode 动态规划问题