描述

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

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

示例 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. 大div套多个小div,怎样设置外div的高度自适应?

    在最后一个div 后面加上 overflow:hidden;如下: <div style="width:580px; height:auto; margin:0 auto; <d ...

  2. easyui datagrid怎么动态获取表头的列名及显示名称

    说明:目前使用easyui combobox多选属性,绑定的数据源是来自datagrid的表头的列名及显示名称 处理方法: //获取冻结的数据源并返回key,value格式数据 var GetFroz ...

  3. # 61条面向对象设计的经验原则-《OOD启思录》Arthur J.Riel

    61条面向对象设计的经验原则-<OOD启思录>Arthur J.Riel 原文 http://blog.csdn.net/cpluser/article/details/129291 61 ...

  4. vue---监听浏览器窗口的宽度

    使用VUE开发后台项目,后台项目需要进行后台根据浏览器窗口进行变化,需要使用vue来监听浏览器的窗口变化. <template>     <div class="conte ...

  5. 泡泡一分钟:Tightly-Coupled Aided Inertial Navigation with Point and Plane Features

    Tightly-Coupled Aided Inertial Navigation with Point and Plane Features 具有点和平面特征的紧密耦合辅助惯性导航 Yulin Ya ...

  6. WMS开发环境

    须安装以下三个软件: JASPER报表开发工具:TIB_js-studiocomm_6.5.1.final_windows_x86_64.exe UI开发工具:Studio_7.0.0.0_win32 ...

  7. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  9. C# .NET 私钥 RSA2,SHA256,签名

    私钥长度2048 -- /// <summary> /// 生成签名 /// </summary> /// <param name="str"> ...

  10. smb文件共享

    一.服务端: #安装 yum install samba samba-common samba-client -y systemctl start smb ##开启samba服务 systemctl ...