Leetcode_Array --581. Shortest Unsorted Continuous Subarray [easy]

本文介绍了一种算法,用于在给定的整数数组中找到一个连续子数组,只要将这个子数组进行升序排序,整个数组就会变为升序。文章提供了两种解决方案,一种是通过遍历数组确定子数组的边界,另一种是先对数组进行排序,然后找出与原数组不同的元素范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Note:
1、Then length of the input array is in range [1, 10,000].
2、The input array may contain duplicates, so ascending order here means <=.

Solution:
这个算法有两种解法。首先分析题目,题目中要求找到一个子数组,只要排序这个子数组,那么整个数组就会被排序好,这使得原数组有一个性质,就是除了这个子数组所在的index范围内,剩余的子数组两边的元素都是按照升序排好的。
所以子数组的边界有这样的性质:子数组的起始位置元素小于其前一个元素,子数组的终止位置元素大于其后一个的元素。所以我们只需要找这样的规律就可以了。
第一种方法就是遍历整个数组,根据上述的规律找子数组的边界。可以想象有两个指针,一个从数组的第一个元素出发,为i指针,另一个从数组的最后一个元素出发,为length-i-1指针;nmax中存储i指针前一个元素与当前元素中的最大值,如果元素不满足规律,那么nmax应该与当前元素不相等,也就是nmax != nums[i] ,代码中就是这样从index 0开始判断,当遍历完整个数组之后就找到了数组中最后一个不满足规律的元素,找到的这个元素就是子元素的终止元素;另一个指针从数组的末尾开始遍历,找到不满足规律的最后一个元素,即nmin != nums[i],这个元素就是子数组的起始位置,然后用终止位置index减去起始位置index即可。
第二种方法就是先将数组排序,子数组的第一个元素必定是首先违背排序规律的元素,莫为元素必定是最后违背排序规律的元素。

Python

(1)
class Solution:
    def findUnsortedSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nmin,nmax = float('inf'),-float('inf') 
        length = len(nums)
        l,r = 0,-1   #r为-1是为了防止给定的数组使升序排列的情况
        for i in range(length):
            nmax = max(nmax,nums[i])
            nmin = min(nmin,nums[length-1-i])
            if nmax != nums[i]:
                r = i   #r为遍历数组后最右边不按顺序排列的元素的index
            if nmin != nums[length-1-i]:
                l = length-1-i   #l中尾遍历数组后最左边不按顺序排列的元素的index
        return r-l+1

(2)
class Solution:
    def findUnsortedSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums1= sorted(nums)
        length = len(nums)
        if nums == nums1:
            return 0
        l = min(i for i in range(length) if nums[i] != nums1[i])
        r = max(length-1-i for i in range(length) if nums[length-1-i] != nums1[length-1-i])
        return r-l+1
C++

class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        int length = nums.size();
        int nmax = INT_MIN;
        int nmin = INT_MAX;
        int l = 0;
        int r = -1;
        for (int i = 0;i<length;i++){
            nmax = max(nums[i],nmax);
            nmin = min(nums[length-1-i],nmin);
            if (nmax != nums[i]){
                r = i;
            }
            if (nmin != nums[length-1-i]){
                l = length-i-1;
            }
        }
        return r-l+1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值