File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+
3+ class Solution :
4+ """
5+ @param nums: A list of integers
6+ @return: A list of integers includes the index of the first number
7+ and the index of the last number
8+ """
9+ def subarraySumClosest (self , nums ):
10+ # write your code here
11+ # 序列a[i + 1]到a[j]的和等于a[0]到a[j]的和减去a[0]到a[i]的和。
12+ if not nums :
13+ return []
14+ sums = []
15+ tmp = 0
16+ for i in xrange (len (nums )):
17+ tmp += nums [i ]
18+ sums .append ([tmp , i ])
19+ sums .sort ()
20+ ret = [0 , 0 ]
21+ diff = 2147483647
22+ for i in xrange (1 , len (sums )):
23+ if abs (sums [i ][0 ] - sums [i - 1 ][0 ]) < diff :
24+ diff = abs (sums [i ][0 ] - sums [i - 1 ][0 ])
25+ ret = []
26+ ret .append (min (sums [i ][1 ], sums [i - 1 ][1 ]) + 1 )
27+ ret .append (max (sums [i ][1 ], sums [i - 1 ][1 ]))
28+ return ret
You can’t perform that action at this time.
0 commit comments