leetcode每日一题581. 最短无序连续子数组20210803

题目

https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/
在这里插入图片描述

答案

自己独立思考的答案

这个方法,时间是和官方题解方法一一样的,但是空间占比多,是因为两层循环,空间消耗更多。
想法就是copy一份数组排序,从两端来判断两个数组的值是否相等,如果不相等视为端点。
这样就是会在时间上快一点,因为不会循环一整个。当然比一层循环的双指针是要慢一点的。

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        int[] sort = new int[nums.length];
        System.arraycopy(nums, 0, sort, 0, nums.length);
        Arrays.sort(sort);
        int begin = -1;
        int end = nums.length;
        for (int i = 0; i < nums.length; i++) {
            if (sort[i] != nums[i]) {
                break;
            }
            begin = i;
        }
        if (begin == nums.length -1) {
            return 0;
        }
        for (int i = nums.length -1; i >= 0; i--) {
            if (sort[i] != nums[i]) {
                break;
            }
            end = i;
        }
        return end - begin - 1;

    }

    public static void main(String[] args) {
        int[] nums = new int[]{2,1};
        new Solution().findUnsortedSubarray(nums);
    }
}

官方题解一优化后的答案

官方题解一,是有先循环判断是否不存在逆序,如果不存在直接返回0.这个时间是n。思考了一下,觉得没有必要,去掉这层单独的循环判断改成while的时候加条件控制,减少了0.1MB内存,超过比例从25%到37%。

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int[] numsSorted = new int[nums.length];
        System.arraycopy(nums, 0, numsSorted, 0, nums.length);
        Arrays.sort(numsSorted);
        int left = 0;
        while (left <= nums.length - 1 && nums[left] == numsSorted[left]) {
            left++;
        }
        if (left == nums.length){
            return 0;
        }
        int right = nums.length - 1;
        while (nums[right] == numsSorted[right]) {
            right--;
        }
        return right - left + 1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4};
        new Solution().findUnsortedSubarray(nums);
    }
}

官方题解二

一层循环双指针。
其实本来我当时也想用双指针来写,但是忘记双指针的写法了。ORZ

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        int maxn = Integer.MIN_VALUE, right = -1;
        int minn = Integer.MAX_VALUE, left = -1;
        for (int i = 0; i < n; i++) {
            if (maxn > nums[i]) {
                right = i;
            } else {
                maxn = nums[i];
            }
            if (minn < nums[n - i - 1]) {
                left = n - i - 1;
            } else {
                minn = nums[n - i - 1];
            }
        }
        return right == -1 ? 0 : right - left + 1;
    }
    
    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4};
        new Solution().findUnsortedSubarray(nums);
    }
}

总结

一层循环必然是比两层循环快的。
只对本身做操作,那空间复杂度就是O(1).
今天这道题也是一步步优化而来。0.1MB的空间优化,却是12%的比例提升。这个比例还是很有参考价值的。
但是就是难。就是难才优秀。看到题解才会惊为天人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值