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. 2015.3.2 VC++6制作非MFC dll以及VS2005、VS2010调用

    1.在VC6中新建工程,选择Win32 Dynamic-Link Libary,输入dll名称如 DLL2015 2.在类型选择中,选择第2项 A Simple Dll project OK 3.随后 ...

  2. ios AppStore 帐号申请

    App Store最新审核指南 https://developer.apple.com/support/app-review/cn/ http://www.woshipm.com/ucd/144218 ...

  3. Plist文件存储

    一.Plilst存储简介 (1)只能储存NSData.NSNumber.NSDictionary.NSString.NSDate.NSArray.BOOL等数据类型,如果需要存储其他NSObject类 ...

  4. 【273】利用ArcPy建立处理数据的脚本

    这个脚本可以直接运行处理程序,首先在 ArcPy 上面测试,成功后写入文件,下面的代码实现将指定文件夹内部的栅格数据进行 Calculate Statistics 操作,否则在进行专题图制作的时候会出 ...

  5. JAVA基础知识总结13(同步)

    好处:解决了线程安全问题. 弊端:相对降低性能,因为判断锁需要消耗资源,还容易产生了死锁. 定义同步是有前提的: 1,必须要有两个或者两个以上的线程,才需要同步. 2,多个线程必须保证使用的是同一个锁 ...

  6. MyBatis总结七:动态sql和sql片段

    开发中,sql拼接很常见,所以说一下动态sql: 1 if 2 chose,when,otherwise 3 where,set 4 foreach 用法解析(现有一张users表 内有id user ...

  7. elasticsearch2.x优化小结(单节点)

    最近es一直卡顿,甚至宕机,用bigdesk看了,才晓得,es一直用的默认配置(可以看出我有多懒,先前数据量小,es足以应付,现在数据量上去后就不行了). 这里总结三方面: 1.提升jvm内存 vi ...

  8. 基于IFC的施工过程模拟程序(4D BIM)

  9. activeMQ功能Demo

    1. 请阐述ActiveMQ的作用 2. 请描述ActiveMQ的工作原理 1. 解决服务之间耦合 2. 使用消息队列,增加系统并发处理量 3. 使用Java程序编写生产者发送10条“你好,activ ...

  10. 安装visual_Paradigm,时序图的应用

    此安装包已经上传到sunny的百度网盘. 删除,即,右击别的地方,然后,选择delete即可. 拖箭头,拖到某个实体上,再松开,会自动连线. 很好的一款画图工具.