描述

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

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

示例 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. /etc/bashrc

    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[3 ...

  2. JQuery selector form input

    var inputPhoneInFormActivity = $("form#formtab input[name='phone']"); if(inputPhoneInFormA ...

  3. 二进制和ASCII文件的区别

    二进制和ASCII文件的区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me 版权声明:本文为CSDN博主「迂者-贺利坚」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出 ...

  4. 【LeetCode算法-21】Merge Two Sorted Lists

    LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...

  5. CSS製作動畫效果(Transition、Animation、Transform)

    CSS 2D Transforms https://www.w3schools.com/css/css3_2dtransforms.asp CSS 3D Transforms https://www. ...

  6. [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. Redis 主从、哨兵Sentinel、Jedis

    Redis 主从.哨兵Sentinel.Jedis 2017年02月15日 15:52:48 有且仅有 阅读数 6183 文章标签: redis主从sentineljedis 更多 分类专栏: 7/1 ...

  8. 【视频开发】RTSP SERVER(基于live555)详细设计

    /* *本文基于LIVE555的嵌入式的RTSP流媒体服务器一个设计文档,个中细节现剖于此,有需者可参考指正,同时也方便后期自己查阅.(本版本是基于2011年的live555) 作者:llf_17@q ...

  9. Vue问题汇总

    Vue——父子组件间异步动态获取数据传递数据时,子组件获取不到值或者延时获取: 通过watch解决:https://blog.csdn.net/where_slr/article/details/99 ...

  10. springmvc+spring+mybatis的整合(推荐新手看看)

    下面开始整合(由于有点多就不一一介绍了) 配置sqlMapConfig.xml(空文件即可) <?xml version="1.0" encoding="UTF-8 ...