Skip to content

Commit f0af670

Browse files
committed
连续子数组求和
1 parent afd4ace commit f0af670

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

continuous_subarray_sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param {int[]} A an integer array
5+
# @return {int[]} A list of integers includes the index of the
6+
# first number and the index of the last number
7+
def continuousSubarraySum(self, A):
8+
# Write your code here
9+
_max = A[0]
10+
_sum = 0
11+
start, end = 0, 0
12+
curr_start = 0 # 记录子数组开始位置
13+
for i in xrange(len(A)):
14+
_sum += A[i]
15+
if _sum > _max:
16+
_max = _sum
17+
start, end = curr_start, i
18+
if _sum < 0:
19+
curr_start = i + 1
20+
_sum = 0
21+
return [start, end]

0 commit comments

Comments
 (0)