581. Shortest Unsorted Continuous Subarray

题目解析

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length.

找到最短的连续子数组,满足子数组有序后整个数组有序。


算法思路

1)i、j,i用于找子数组起始位置,j遍历整个数组并且找子数组结束位置;

2)minSub、maxSub为子数组中的最大最小值,每当i、j移动都需要判断两个变量并赋值;

3)结果子数组需要满足的条件,子数组中最小值minSum>=nums[i-1],子数组中的最大值maxSum<=nums[j+1];

4)在该循环里j不断向后遍历,但每当minSub的值改变需要重新判断i的位置;

5)跳出循环的条件,j遍历到数组结尾,同时满足minSum>=nums[i-1]。

ps:这个循环条件调试了好久啊!!

你可能感兴趣的:(581. Shortest Unsorted Continuous Subarray)