描述

给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。

你找到的子数组应是最短的,请输出它的长度。

示例 1:

输入: [2, 6, 4, 8, 10, 9, 15]
输出: 5
解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
说明 :

输入的数组长度范围在 [1, 10,000]。
输入的数组可能包含重复元素 ,所以升序的意思是<=。

解析

排序

先排序,再比较左、右的第一个不一致的index即可。

巧思

看代码即可。

代码

//[2, 6, 4, 8, 10, 9, 15]
public int findUnsortedSubarray(int[] nums) {
if (null == nums || nums.length <= 0) {
return 0;
}
int min = Integer.MAX_VALUE;//最小的一个:后一个数大于前一个数的 从左往右遍历. 即上面的4
int max = Integer.MIN_VALUE;//最小的一个:前一个数大于后一个数的 从右往左遍历. 即上面的10
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
min = Math.min(min, nums[i]);
}
}
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1]) {
max = Math.max(max, nums[i]);
}
}
int left = 0, right = 0;
//找到第一个大于min的值,即为最左边的应该排序的值
for (int i = 0; i < nums.length; i++) {
if (nums[i] > min) {
left = i;
break;
}
}
//找到第一个小于max的值,即为最右边的应该排序的值
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] < max) {
right = i;
break;
}
}
return right - left <= 0 ? 0 : right - left + 1;
}

[LeetCode] 581. 最短无序连续子数组 ☆的更多相关文章

  1. Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)

    581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...

  2. Leetcode 581.最短无序连续子数组

    最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...

  3. LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)

    581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...

  4. [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  5. leetcode面试题42. 连续子数组的最大和

      总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目   面试题42. 连续子数 ...

  6. C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)

    问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...

  7. [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  8. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  9. [LeetCode] Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

随机推荐

  1. canvas api 速记

    基本骨骼 <canvas id="canvas" width=1000 height=1000 style="border: 1px black dotted&qu ...

  2. https://www.cnblogs.com/LBSer/p/3310455.html

    https://www.cnblogs.com/LBSer/p/3310455.html

  3. Java基础 三目运算符 用if-else对其进行解释

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  4. Nodejs介绍和环境搭建

    Nodejs是JavaScript的运行环境,它让 Java Script 可以开发后端程序,实现几乎其他后端 Node.js 是一个 Javascript 运行环境(runtime) 可以与 PHP ...

  5. mysql Last_SQL_Errno: 1197 Coordinator stopped because there were error(s) in the worker(s)问题处理

    Last_SQL_Errno: 1197 Coordinator stopped because there were error(s) in the worker(s). The most rece ...

  6. OpenShift上的OpenvSwitch入门

    前段时间参加openshift培训,通过产品部门的讲解,刷新了我对OpenShift一些的认识,今天先从最弱的环节网络做一些了解吧. Openvswitch是openshift sdn的核心组件,进入 ...

  7. Spring MVC -- Spring MVC入门

    本篇博客首先介绍Spring MVC的优点,然后介绍Spring MVC的基本组件,包括DispatcherServlet,并学习如何开发一个“传统风格”的控制器,这是在Spring 2.5版本之前开 ...

  8. vue前端项目中excel文件下载

    模仿 https://github.com/PanJiaChen/vue-element-admin/ 的下载 创建文件夹vendor 创建文件 Export2Excel.js 内容: /* esli ...

  9. Redis哨兵(Sentinel)模式

    Redis哨兵(Sentinel)模式   主从切换技术的方法是:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力,还会造成一段时间内服务不可用.这不是一种推荐的方式 ...

  10. 使用transform后z-index失效的解决方法

    transform作用的元素增加translateZ,父级元素增加 transform-style: preserve-3d; <div class="father"> ...