给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
你找到的子数组应是最短的,请输出它的长度。
示例 1:
输入: [2, 6, 4, 8, 10, 9, 15] 输出: 5 解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。 说明 :
输入的数组长度范围在 [1, 10,000]。 输入的数组可能包含重复元素 ,所以升序的意思是<=。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 我的解:
class Solution {public int findUnsortedSubarray(int[] nums) {if(nums.length < 2) return 0;int left = 0;int right = nums.length - 1;while (left + 1 < nums.length && nums[left] <= nums[left + 1]) left++;while (right - 1 > -1 && nums[right] >= nums[right - 1]) right--;if (left > right) return 0;int min = Integer.MAX_VALUE;int max = Integer.MIN_VALUE;for (int i = left; i <= right; i++){if (nums[i] > max) max = nums[i];else if (nums[i] < min) min = nums[i];}int p = 0;while (nums[p] <= min) p++;left = p;p = nums.length-1;while (nums[p] >= max) p--;right = p;return right - left + 1;}}

