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.

Example 1:

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

思路:第一遍从左往右扫,看当前值是否比找到的最大值还小,如果是这样的话,明显中间不是递增的。同样,从右往左扫,如果当前值比已经找到的最小值大,那么中间部分也不是递增的。
 1 class Solution {
 2     public int findUnsortedSubarray(int[] nums) {
 3         if (nums == null || nums.length == 0 || nums.length == 1) return 0;
 4         int max = Integer.MIN_VALUE, end = -1;
 5         // iterate from beginning of array find the last element which is smaller than
 6         // the last seen max from its left side and mark it as end
 7         for (int i = 0; i < nums.length; i++) {
 8             max = Math.max(max, nums[i]);
 9             if (nums[i] < max)
10                 end = i;
11         }
12         if (end == -1) return 0;
13
14         int min = Integer.MAX_VALUE, begin = -1;
15         for (int i = nums.length - 1; i >= 0; i--) {
16             min = Math.min(min, nums[i]);
17             if (nums[i] > min)
18                 begin = i;
19         }
20         return end - begin + 1;
21     }
22 }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/11371269.html

时间: 2024-11-02 03:29:06

Shortest Unsorted Continuous Subarray的相关文章

LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)

581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. LeetCode581. Shortest Unsorted Continuous Subarray 示例 1: 输入: [2, 6, 4, 8, 10, 9, 15] 输出: 5 解释: 你只需要对 [6, 4, 8,

Shortest Unsorted Continuous Subarray LT581

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. E

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. E

[LeetCode] 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. E

LeetCode - 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. E

581. Shortest Unsorted Continuous Subarray (LeetCode)

Description: 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

581. Shortest Unsorted Continuous Subarray

1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 int findUnsortedSubarray(vector<int>& nums) 12 { 13 int sz=nums.size(); 14 int beg=-1,end=-2,maxele=nums[0],m

LeetCode算法题-Shortest Unsorted Continuous Subarray(Java实现)

这是悦乐书的第267次更新,第281篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第134题(顺位题号是581).给定一个整数数组,找到一个连续的子数组,按升序对该子数组进行排序,使得整个数组也按升序排序.找到最短的无序连续子数组并输出其长度.例如: 输入:[2,6,4,8,10,9,15] 输出:5 说明:按升序对[6,4,8,10,9]子数组进行排序,以使整个数组按升序排序. 注意: 数组的长度在[1,100]范围内. 数组可能包含重复项,因此这里的升序表示<=

Continuous Subarray Sum

Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone) 和Maximum Subarray的差别在于仅