16.Shortest Unsorted Continuous Subarray(最短未排序子数组)
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:
- Then length of the input array is in range [1, 10,000].
- 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(最短未排序子数组)的更多相关文章
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8, 1 ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- 【leetcode_easy】581. Shortest Unsorted Continuous Subarray
problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...
- 581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...
- LeetCode算法题-Shortest Unsorted Continuous Subarray(Java实现)
这是悦乐书的第267次更新,第281篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第134题(顺位题号是581).给定一个整数数组,找到一个连续的子数组,按升序对该子数组 ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)
问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...
随机推荐
- 2015广州强网杯(Misc)
单身狗: 下载图片 被一只狗挡住了的二维码,用图片处理软件把上面两个正方形随便一个覆盖狗的地方 我直接用美图秀秀处理一下,扫一下就得到flag
- 10-17C#语句(3)--跳转语句、异常处理语句
回顾: 穷举法(重点掌握):虽然运用for...嵌循环语句,但是也要找到执行for...循环的规律, 即一个题目中,需要得到哪个值,首先定义它初始变量:哪个条件需要改变,它对应的就是for...循环的 ...
- 浅谈Android四大组建之一Service---Service的创建
Service是安卓四大组件之一,个人觉得Service的使用还是比较简单的额,可以理解为看不见的Activity,因为Service的使用和Activity十分接近.启动啊,生命周期等,都十分简单. ...
- 常见浏览器bug以及解决方法
1.图片下方3像素: (1).描述:在div中插入图片时,图片会将div下方撑大三像素. (2).hack1:将</div>与<img>写在一行上(可以解决ie6/7): (3 ...
- hbase.client.RetriesExhaustedException: Can't get the locations hive关联Hbase查询报错
特征1: hbase.client.RetriesExhaustedException: Can't get the locations 特征2: hbase日志报错如下:org.apache.zoo ...
- mysql存储过程@命名变量的区别
存储过程中@是用来标识每一次运行存储过程都会保存的值.而直接命名是每一次都会初始化的局部变量.@@是用来标识全局变量. CREATE PROCEDURE prc_test ()BEGIN DECLAR ...
- re.spilt
- day70-oracle 13-数据字典
实际上数据字典它就是表.这种表比较特殊,给它取个名字叫做数据字典.既然是表的话,它就是要存数据的.它存的是这些数据:用户有哪些权限,用户创建了哪些表,用户能够访问哪些表,这种信息跟员工表.部门表没有关 ...
- c# 调用系统默认图片浏览器打开图片
private void OpenImage(string fileName) { try { Process.Start(fileName); } catch (Exception ex) { // ...
- Gstreamer编程
一.简介 GStreamer是一个开源的多媒体框架库.利用它,可以构建一系列的媒体处理模块,包括从简单的ogg播放功能到复杂的音频(混音)和视频(非线性编辑)的处理.应用程序可以透明的利用解码和过滤技 ...