What is the shortest unsorted continuous subarray problem in LeetCode 581?

2026-06-10 04:0110阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计428个文字,预计阅读时间需要2分钟。

What is the shortest unsorted continuous subarray problem in LeetCode 581?

给定一个整数数组,你需要找到一个连续的子数组。如果对这个子数组进行升序排序,那么整个数组也将是升序的。你需要找到最短的这样的子数组。


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.

What is the shortest unsorted continuous subarray problem in LeetCode 581?

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.

Note:

  • Then length of the input array is in range [1, 10,000].
  • The input array may contain duplicates, so ascending order here means <=.
  • 分析

    题目的意思是:给定一个数组,找出一个连续子数组,如果这个子数组递增,那么整个数组就递增。

    • 从前往后寻找最大值,如果遇见元素小于前面的最大值,我们找到右边界;从后往前寻找最小值,如果遇见的元素大于前面的最小值,我们找到左边界。

    代码

    class Solution {
    public:
    int findUnsortedSubarray(vector<int>& nums) {
    int len=nums.size();
    int min_value=nums[len-1];
    int max_value=nums[0];
    int r=-1;
    int l=0;
    for(int i=0;i<nums.size();i++){
    max_value=max(nums[i],max_value);
    if(nums[i]<max_value){
    r=i;
    }
    min_value=min(nums[len-1-i],min_value);
    if(nums[len-1-i]>min_value){
    l=len-1-i;
    }
    }
    return r-l+1;
    }
    };

    参考文献

    ​​581. Shortest Unsorted Continuous Subarray​​


    本文共计428个文字,预计阅读时间需要2分钟。

    What is the shortest unsorted continuous subarray problem in LeetCode 581?

    给定一个整数数组,你需要找到一个连续的子数组。如果对这个子数组进行升序排序,那么整个数组也将是升序的。你需要找到最短的这样的子数组。


    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.

    What is the shortest unsorted continuous subarray problem in LeetCode 581?

    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.

    Note:

  • Then length of the input array is in range [1, 10,000].
  • The input array may contain duplicates, so ascending order here means <=.
  • 分析

    题目的意思是:给定一个数组,找出一个连续子数组,如果这个子数组递增,那么整个数组就递增。

    • 从前往后寻找最大值,如果遇见元素小于前面的最大值,我们找到右边界;从后往前寻找最小值,如果遇见的元素大于前面的最小值,我们找到左边界。

    代码

    class Solution {
    public:
    int findUnsortedSubarray(vector<int>& nums) {
    int len=nums.size();
    int min_value=nums[len-1];
    int max_value=nums[0];
    int r=-1;
    int l=0;
    for(int i=0;i<nums.size();i++){
    max_value=max(nums[i],max_value);
    if(nums[i]<max_value){
    r=i;
    }
    min_value=min(nums[len-1-i],min_value);
    if(nums[len-1-i]>min_value){
    l=len-1-i;
    }
    }
    return r-l+1;
    }
    };

    参考文献

    ​​581. Shortest Unsorted Continuous Subarray​​