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

FAST-LIO2实战:从零搭建香港大学火星实验室SLAM框架(附官方数据集测试指南) 本文提供了一份详细的FAST-LIO2实战指南,帮助读者从零开始搭建香港大学火星实验室开源的这一高效激光惯性里程计框架。内容涵盖系统环境配置、依赖安装、源码编译、官方数据集测试以及参数调优,旨在让开发者快速上手并应用于机器人SLAM项目。 阅读详情

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记录当前后退的索引。
  • 当一次遍历完成,前进的索引记录了不符合升序规则的最大索引,后退的索引记录了不符合规则的最小索引。
  • 注意在给i和j赋初值的时候要考虑数组元素全部按升序排序的情况,返回为0。所以,赋值i和j为不大于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)
在 SAP 中,Primary Cost Element(初级成本要素)与Secondary Cost Element(次级成本要素)的核心区别、作用及业务含义可归纳如下 摘要:SAP中初级成本要素与次级成本要素的核心区别在于:初级要素必须关联FI模块的损益科目,直接影响财务报表,用于记录外部成本(如电费、薪酬);次级要素仅存在于CO模块,用于内部成本分配(如分摊、作业分配),不影响外部报表。初级要素是"外部成本入口",次级要素是"内部成本搬运工",二者共同实现成本的多维度追踪与管理。 阅读详情

相关推荐

逐行逐句进一步了解SurroundOcc(一)

通过分析中的模型构建过程,理解了从配置文件到模型实例化的整个流程。构建过程的核心是通过build和等函数,逐步将配置文件解析并生成模型的各个模块对象,最终组合成一个完整的模型结构。这一过程涉及多个类和函数的调用,其中函数是生成对象的关键,它能够从配置字典中提取必要信息并生成对应的类实例。

lovely_yoshino的博客 8126

数组的排序&查找

HBU OJ004 冒泡排序(Bubble Sort) 也称为沉降排序(Sinking Sort),之所以称其为冒泡排序,是因为算法中值相对较小的数据会像水中的气泡一样逐渐上升到数组的最顶端。与此同时,较大的数据逐渐地下沉到数组的底部。这个处理过程需在整个数组范围内反复执行多遍。每一遍执行时,比较相邻的两个元素。若顺序不对,则将其位置交换,当没有数据需要交换时,数据也就排好序了。编程将排序函数Da...

绀香零八的博客 2459

RC滤波电路工作原理:【带图+公式计算】

容抗Xc和电容C成反比,和频率f也成反比,C越大,交流电容易通过电容,说明电容量大,电容的阻碍作用小;交流电的频率高,交流电也容易通过电容,说明频率高,电容的阻碍作用也小。我们可以看到,一个2V的纹波,经常RC电路后,幅值下降到mv级别,基本可以忽略不计,这就是RC滤波电路的工作原理。一个频率为1Khz的信号,通过一个容值为0.1uf的电容,计算电容对该信号产生的阻抗。将RC滤波器电路中的电阻,取值为1KΩ,电容取值为0.47uf,纹波幅值为2V。从公式1可以看到,Xc越大,Vout值就越接近Vin的值。

weixin_66299220的博客 2万+

工大SSE(中等难度)

sse

weixin_45535091的博客 5141

ubuntu打实时内核补丁教程以及安装后显卡驱动问题解决方法

在ubuntu20.04上安装成功,亲测有效 目录ubuntu打实时内核补丁教程1、下载实时内核2、解压和打补丁(patch)3、安装依赖和编译安装4、修改一些文件,便于之后切换内核5、重启电脑实时内核版本下安装nvidia显卡驱动的问题1、先切换回之前的内核(非实时)版本2、重装之前内核(非实时)版本下的显卡驱动3、切换回实时内核(按照上面的步骤)4、在实时内核下安装显卡驱动 ubuntu打实时内核补丁教程 安装目标系统:ubuntu20.04 电脑配置:i9,2080ti 1、下载实时内核 打开终端,输

weixin_39275295的博客 8009

【HAL库】STM32CubeMX开发----STM32F407----ETH+LWIP----TCPclient客户端

STM32F407开发板 为TCPclient客户端,PC端 为TCPserver服务端,相互之间的发送和接收实验。

MQ0522的博客 3055

将10个整数进行升序排序

将10个整数进行升序排序

2301_81485935的博客 1581

基于ComfyUI+H3的本地化竖屏短剧自动化生成工作流

本文介绍了一种基于ComfyUI和MiniMax H3的本地化竖屏短剧自动化生成工作流。该方案通过纯机械读取分镜总纲表格,实现从文本到视频的全自动生成,支持多人同框稳定输出、人脸一致性保持、音视频同步生成等功能。工作流包含图像生成、人工审核、视频生成和音频处理四个核心环节,采用本地化部署方案避免云端服务的限制和成本问题。文章详细说明了环境部署步骤、必需的自定义节点安装以及显存优化配置,特别针对16GB显存设备提供了量化压缩、GPU独占调度等技术方案,确保系统流畅运行。

huanghm88的专栏 370

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

xdhc的博客 288

Deep SHAP 深度学习模型的可解释性分析

Deep SHAP 算法是一种用于解释深度学习模型的技术, 该算法通过将每个特征的重要性与输出值相关联来进行解释。本博客对深度学习的模型可解释性进行了分析。

cnjs1994的博客 5131

系统工程:从原理到实践的核心要点

系统工程是一门综合性学科,旨在解决复杂工程问题,确保项目在全生命周期内高效、可靠地运行。本文将深入探讨系统工程的概述、方法、生命周期、核心要点以及总结,帮助读者全面理解和应用系统工程的理论与实践。

658

3. 最大数

重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。返回值类型为void以及函数体代码可以推导出返回值类型可以不写。输出结果可能非常大,所以你需要返回一个字符串而不是整数。包含该头文件可以直接使用转换函数to_string()[变量使用说明符号] (参数列表)2.C++11Lambda表达式。

m0_73673820的博客 352

31、MSP430FR2355时钟系统与低功耗模式解析

本文详细解析了MSP430FR2355的时钟系统和低功耗模式。首先介绍了时钟系统的组成,包括XT1振荡器、DCO、MODCLK、MCLK、SMCLK和ACLK的功能与配置方法。接着探讨了MSP430FR2355的五种低功耗模式及其配置要点,并分析了时钟系统与低功耗模式之间的协同关系。最后通过实际应用案例展示了如何在具体场景中优化系统功耗。文章旨在帮助开发者充分利用MSP430FR2355的时钟与低功耗特性,实现高性能、低功耗的嵌入式系统设计。

mars5的博客 207

【leetcode_easy】581. Shortest Unsorted Continuous Subarray

problem 581.Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白。 solution1: 使用一个辅助数组,新建一个跟原数组一模一样的数组,然后排序。从数组起始位置开始,两个数组相互比较,当对应位置数字不同的时候停止,同理再从末尾开始,对应位置上比较,也是遇到不同的数字时停止,这样中间一段就是最短无序连续子数组。 ...

weixin_30790841的博客 115

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode Top 100 Liked Questions 581. Shortest Unsorted Continuous Subarray (Java版; Easy)

welcome to my blog LeetCode Top 100 Liked Questions 581. Shortest Unsorted Continuous Subarray (Java版; Easy) 题目描述 Given an integer array, you need to find one continuous subarray that if you only sort...

littlehaes的博客 166

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode OJ 581 Shortest Unsorted Continuous Subarray [Easy]

题目描述: Given an integerarray, you need to find one continuous subarray that if you only sort thissubarray in ascending order, then the whole array will be sorted in ascendingorder, too. You need to

大吱的博客 315

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 581. Shortest Unsorted Continuous Subarray

Given an integer array, you need to find onecontinuous subarraythat if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to ...

dianguju2418的博客 111

【LeetCode】581. Shortest Unsorted Continuous Subarray

581. Shortest Unsorted Continuous Subarray 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 w...

Freemanzxp 179

leetcode 581. Shortest Unsorted Continuous Subarray (easy)

一开始的思路是: 分别从两边往中间寻找,分别找到第一个排序异常的点,这两个点之间的子串就是所求子数组。 在实现上述过程时候,发现其实可以简化成这样的思路: 将nums分为三个部分,nums[0…i]是正序,nums[i…j]是所求的子数组,nums[j…end]又是正序。 实现部分:保存左边最大值,保存右边最小值。 向右遍历时:出现比左最大值小的,就是异常点; 向左遍历时:出现比右最小值大的,就是...

Rh's blog 408

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]子数组进行排序,以使整个数...

小川的博客 194

【Leetcode-Easy-581】Shortest Unsorted Continuous Subarray

【Leetcode-Easy-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 w

LYZ 797

【LeetCode】581.Shortest Unsorted Continuous Subarray(easy)解题报告

【LeetCode】581.Shortest Unsorted Continuous Subarray(easy)解题报告题目地址:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/ 题目描述:   Given an integer array, you need to find on

郝春雨的博客 300

【python3】leetcode 581. Shortest Unsorted Continuous Subarray(easy)

 581. Shortest Unsorted Continuous Subarray(easy) 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 b...

momottyy的专栏 344

【Leetcode】581. Shortest Unsorted Continuous Subarray(Easy)

1.题目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 fin...

不想做菜鸟的小螃蟹 286

leetcode 581[easy]---- Shortest Unsorted Continuous Subarray

难度:easy 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. Yo

fawnzou2017的博客 226

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

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. 给定一个整数数组,找到一个连续子数组...

Ling的博客 225
上一篇: LeetCode-566:Reshape the Matrix (矩阵整型)-- easy
下一篇: LeetCode-717:1-bit and 2-bit Characters (1位和2位编码元素)-- easy
大树先生的博客
博客等级 码龄9年 2692粉丝 200原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址