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, ...
随机推荐
- Celery-4.1 用户指南: Daemonization (系统守护进程)
Generic init-scripts 查看Celery发布里的 extra/generic-init.d/ 文件夹. 这个文件夹中包含了celery worker 程序的通用bash初始化脚本,可 ...
- 问题:oracle case when;结果:Oracle CASE WHEN 用法介绍
Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...
- Samba服务学习报错总结
1 2 3 4 5 此文献来至百度文库 http://wenku.baidu.com/link?url=hkHembjXcjoYRU9ky34a46Lzv5SAEutwa0v1_F8INQsdg_KK ...
- liunx环境,摄像头无法识别,解决方案
今天无语了,linux14.04系统下,使用罗技c270摄像头.发现插上没有反应,系统版本: lenovo-myc@lenovomyc-Lenovo-Product:~/Downloads$ unam ...
- MyBatis使用动态代理报 invalid bound statement (not found) 错
这个问题网上大部分都说xml文件中的路径不对 或者是resources之类的问题,如果那些文章的解决方案解决不了你的问题的话,可以看一下我遇到的这种情况: 前提: mybatis-config.xml ...
- 3-2 zk客户端连接关闭服务端,查看znode
使用ZooKeeper官方提供的Client来连接.路径类似的结构. 连接到我们的门户HOST. quota属于zookeeper.quota是子节点,zookeeper是父节点.quota其实是一个 ...
- powerdesigner自动将name填充到注释的脚本
我在建模的时候,希望在生成脚本的时候有注释,所以才会看到Comment列,实际上,只要你的表中的Name列不为空,运行下面的VBScript,PD会帮你自动填充注释的Comment列值. '把pd中那 ...
- eclipse中安装git插件
1 安装及配置git插件,问度娘即可 点击前往 2 eclipse 中怎么同步到 本地git仓库 和 码云远程仓库 点击前往
- Verilog 语言 001 --- 入门级 --- 编写一个半加器电路模块
Verilog 语言编写一个 半加器 电路模块 半加器 的电路结构: S = A 异或 B C = A 与 B 1. 程序代码 module h_adder (A, B, SO, CO); input ...
- 分享常用的GoLang包工具
包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/eclipse/paho.mqt ...