LeetCode-581:Shortest Unsorted Continuous Subarray (最短未排序连续子数组) -- easy

Question

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:

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

问题解析:

给定整数数组,找到一个最短的连续的未按照升序排序的最短子数组,返回子数组的长度。

Answer

Solution 1:

排序比较。

  • 一种较为直观的想法,先将数组排序赋给一个新的数组,通过两个数组之间的比较,第一个和最后一个元素不同的位置索引即为子数组的界限。
  • 这里用Python来实现,先将数组排序,后用zip(old, new)来比较排序前后数组元素之间是否一一对应相等;
  • 数组总长度减去正序的一个为False的位置和逆序第一个为False的位置。
class Solution(object):
    def findUnsortedSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        sort_nums = sorted(nums)
        is_same = [a == b for a, b in zip(nums, sort_nums)]
        if all(is_same):
            return 0
        else:
            return len(nums) - is_same.index(False) - is_same[::-1].index(False)
Solution 2:

一般的比较方法。

  • 按照我们自己寻找最小子数组的思路来解决。
  • 首先分别设置左、右指针来指示子数组的首和尾,并设置最大值和最小值,(最小值赋大值,最大值赋小值);
  • 分别左右两方向寻找不符合递增规则的边界索引,期间若左指针l最后等于数组最大索引,则证明数组全部元素均按升序排序;
  • 在找到的界限内寻找内部的最小值和最大值;
  • 左指针向左走,右指针向右走,直到小于界限内最小值后的位置,及大于界限内最大值前的位置。
  • 注意处理好最后返回值。
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int l = 0, r = nums.length - 1, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;

        while (l < r && nums[l] <= nums[l+1]) l++;

        if (l == r) return 0;

        while (nums[r] >= nums[r-1]) r--;

        for (int k = l; k <= r; k++){
            max = Math.max(max, nums[k]);
            min = Math.min(min, nums[k]);
        }

        while (l >= 0 && min < nums[l]) l--;
        while (r <= nums.length-1 && max > nums[r]) r++;

        return r-l-1;

    }
}
  • 时间复杂度:O(n),空间复杂度:O(1)
Solution 3:

更简洁的方法。

  • 一次遍历,左、右同时进行;
  • 左边前进记录当前经过元素的最大值,若按照升序规则,则当前遍历元素即为当前最大值;如果二者不相等,则用j记录当前前进的索引;
  • 右边后退记录当前经过元素的最小值,按照升序规则,则当前遍历元素即为当前最小值;如果二者不相等,则用i记录当前后退的索引。
  • 当一次遍历完成,前进的索引记录了不符合升序规则的最大索引,后退的索引记录了不符合规则的最小索引。
  • 注意在给ij赋初值的时候要考虑数组元素全部按升序排序的情况,返回为0。所以,赋值ij为不大于0且相差1,如:i = 0, j = -1,或i = -1, j = -2
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int i = 0, j = -1, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;

        for (int l = 0, r = nums.length-1; r >= 0; l++, r--){
            max = Math.max(max, nums[l]);
            if (nums[l] != max) j = l;

            min = Math.min(min, nums[r]);
            if (nums[r] != min) i = r;
        }

        return (j - i + 1);
    }
}
  • 时间复杂度:O(n),空间复杂度:O(1)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值