题目
思路
trick。如果 0−i 0 − i 的和等于 0−j 0 − j 的和,那么 i−j i − j 的和为0。
代码
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
tmp_dict = {}
tmp_dict[0] = -1
sum = 0
for i, v in enumerate(nums):
sum += v
if sum in tmp_dict:
res_list = []
res_list.append(tmp_dict[sum] + 1)
res_list.append(i)
return res_list
tmp_dict[sum] = i