给定一个整数数组,你需要找到一个连续的子数组,如果你只按升序对这个子数组进行排序,那么整个数组也将按照升序排序。
你需要找到最短的这种子阵列并输出它的长度。
排序后比对不相同的数段
class Solution(object):
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
s = e = -1
snums = sorted(nums)
for i in range(len(nums)):
if snums[i] != nums[i]:
if s == -1:
s = i
e = i
return e-s+1 if e != s else 0