Level:

  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.

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:

  1. Then length of the input array is in range [1, 10,000].
  2. The input array may contain duplicates, so ascending order here means <=.

思路分析:

  思路一:对数组进行排序,然后和原数组进行比较,从前到后找到第一个不同的,然后从后往前找到第一个同的,那么差就是结果。时间复杂度为O(nlgn)。

  思路二:根据题意子数组的start和end需要满足的条件是,start-1后面的数都要比该点大,end+1前面的数都要比该点小。因此先找到第一个比start-1点小的数,然后搜索后面的数保证都比起大,遇见小的start往前移,同样的从先找到第一个比end+1点大的点,然后往前搜索如果有比他大的,则end+1要后移;

代码:

思路一:

class Solution {
public int findUnsortedSubarray(int[] nums) {
int i,j;
int []temp=nums.clone();
Arrays.sort(nums);
for(i=0;i<nums.length;i++){
if(nums[i]!=temp[i])
break; }
for(j=nums.length-1;j>=0;j--){
if(nums[j]!=temp[j])
break;
}
if(i==nums.length&&j==-1)
return 0; else
return j-i+1;
}

思路二:

public class Solution{
public int findUnsortedSubarray(int []nums){
//先找start
int pos=1;
while(pos<nums.length&&nums[pos]>=nums[pos-1])
pos++;
if(pos==nums.length)//证明全部都是有序的
return 0;
int start=pos-1;
while(pos<nums.length){
while(start>=0&&nums[start]>nums[pos])//遇见后面比其小的start位置就得前移
start--;
if(start==-1)//子数组从第一个元素开始
break;
pos++;
}
//找end
pos=nums.length-2;
while(pos>=0&&nums[pos]<nums[pos+1])
pos--;
if(pos==-1)
return 0;
int end=pos+1;
while(pos>=0){
while(end<nums.length&&nums[end]<nums[pos])//遇见前面比其大的end位置就得后移
end++;
if(end==nums.length)
break;
pos--;
}
return end-start-1;
}
}

16.Shortest Unsorted Continuous Subarray(最短未排序子数组)的更多相关文章

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

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

  2. Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组

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

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

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

  4. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

    problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...

  5. 581. Shortest Unsorted Continuous Subarray

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

  6. LeetCode算法题-Shortest Unsorted Continuous Subarray(Java实现)

    这是悦乐书的第267次更新,第281篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第134题(顺位题号是581).给定一个整数数组,找到一个连续的子数组,按升序对该子数组 ...

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

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

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

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

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

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

随机推荐

  1. 如何设置select下拉禁止选择

    转自:https://blog.csdn.net/you23hai45/article/details/52233207

  2. 【266】增加bash文件的执行权限

    正常需要通过[bash pass.sh]来执行文件,但是可以通过增加bash文件的执行权限实现通过[./pass.sh]或者[pass.sh]来执行文件. 方法:通过chmod来增加权限,下面四种方法 ...

  3. myeclipse.ini

    myeclipse10 32位 我的配置 #utf8 (do not remove) #utf8 (do not remove) -startup ../Common/plugins/org.ecli ...

  4. 从公交塞车,看C#多线程问题(转)

    好久没写博客了,可能是因为最近工作太过于压抑的原因吧!有点颓废了.... 而且公司距离住处要坐公交将近40--50分钟(各个原因,纠结中ing...),提前一个半小时起床,居然还能迟到!因为距离公司前 ...

  5. Win10系统下安装360安全卫士,安装完成后一直提示

     新买的电脑,第一次安装360安全卫士,安装完成后一直报这个错误,显示MiniUI.dll文件不存在,但这个文件就在安装目录下:后面有多次卸载.安装都能能成功,求助大神        

  6. Blender 安装

    Blender 安装 Blender 安装 windows 上安装 Blender 搞定 Ubuntu Linux 上安装 Blender 搞定 windows 上安装 Blender Step 1 ...

  7. 前端学习笔记2017.6.21-引入JS文件的方法

    通过网络引入JS文件 <script src="https://www.xxx.com/aaa.js"></script> 如果是引入本地JS文件 < ...

  8. noi.ac day3t2 染色

    传送门 分析 dp[i][j]为考虑前i个位置,[i-j+1,i]中的颜色互不相同,并且ai-j与这段区间中的某一个位置颜色相同 我们枚举第i+1个位置和[i-j+1,i]中的哪一个颜色相同或者全部不 ...

  9. debug---null Pointer Exception--一步步查找(2)

    添加PartyLocationRepository后,再次在Ubuntu中编译项目,再次报空指针异常. 直接在createDto处打断点,然后debug每个表达式的值,找出来到底是哪个为null. 经 ...

  10. C++面试笔记--循环链表,队列,栈,堆

    之前已经学会了单链表的建立删除插入转置以及一些普通操作,双链表和单链表差不多,就是多了一个前驱指针,在许多操作中很方便,但是加了一个指针开销应该会大一些,总体上影响不大,这里开始讨论循环链表以及其他的 ...