-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path581.py
More file actions
44 lines (33 loc) · 1.15 KB
/
581.py
File metadata and controls
44 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__author__ = "vcancy"
# /usr/bin/python
# -*-coding:utf-8-*-
"""
分析:需要找到无序连续子数组的起始位置和结束位置
起始位置:从右到左找到比前面的最小值还大的数
结束位置:从左到右找到比前面的最大值还小的数
eg. [1,3,2,4,5]
起始位置从5开始,直到3,比前面的最小值2还大了,起始位置就是它.
结束位置从1开始,直到2,比前面的最大值3还小了,结束位置就是它.
"""
class Solution:
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
start = 0
end = 0
mi = nums[-1]
ma = nums[0]
n = len(nums)
for i in range(n):
mi = min(mi, nums[n - 1 - i])
ma = max(ma, nums[i])
if mi < nums[n - 1 - i]: # 从右到左找到比前面的最小值还大的数
start = n - 1 - i
if ma > nums[i]: # 结束位置:从左到右找到比前面的最大值还小的数
end = i
if start == 0 and end == 0:
return 0
else:
return end - start + 1