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],minele=nums[sz-1];
15         for(int i=1;i<sz;i++)
16         {
17             maxele=max(maxele,nums[i]);
18             minele=min(minele,nums[sz-1-i]);
19             if(nums[i]<maxele) end=i;
20             if(nums[sz-1-i]>minele) beg=sz-1-i;
21         }
22         return end-beg+1;
23     }
24 };

这个方法很骚,线性时间,解释一波

从前向后扫描,maxele存放从nums[0]到nums[i]之间的最大元素,和当前元素比较,若当前元素nums[i]小于这个最大值,则更新end

从后往前扫描,minele存放从nums[sz-1]到当前元素中的最小值,和当前于元素比较,若当前元素大于这个最小值,则更新beg

1  2  3  6  4  5  7  8  9

       beg          end

再说明一下就是,

只有前面的元素是排序后的最终元素时,beg才不会向前移动。如果不是最终位置,必然会有某个元素会大于minele,然后更新beg

只有后面的元素是排序后最终元素时,end才不会向后移动。如果不是最终位置,必然会有某个元素小于maxele,然后更新end

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9141513.html

时间: 2024-07-29 00:42:55

581. Shortest Unsorted Continuous Subarray的相关文章

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

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

[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

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(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的差别在于仅