最短无序连续子数组
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
你找到的子数组应是最短的,请输出它的长度。
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
sorted_nums = sorted(nums)
start_index, end_index = len(nums), 0
for i in range(len(nums)):
if sorted_nums[i] != nums[i]:
start_index = min(start_index, i)
end_index = max(end_index, i)
else:
pass
if start_index > end_index:
return 0
else:
return end_index - start_index + 1