LintCode 138. Subarray Sum
程序员文章站
2022-03-24 17:38:20
...
题目
思路
trick。如果的和等于的和,那么的和为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
上一篇: Spark学习笔记:Spark基础
推荐阅读
-
LintCode 722: Maximum Subarray VI (Trie, 异或经典难题)
-
HK Maximum Subarray Sum
-
LintCode 139. Subarray Sum Closest
-
Minimum Size Subarray Sum
-
Minimum Size Subarray Sum
-
lintcode-44. Minimum Subarray
-
LeetCode 560 Subarray Sum Equals K (hash)
-
Leetcode0523--Continuous Subarray Sum 连续和倍数
-
LeetCode-209-Minimum Size Subarray Sum
-
【Lintcode】905. Nested List Weight Sum II