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.
class Solution {
public int findUnsortedSubarray(int[] nums) {
int n=nums.length,end=-2,start=-1,max=nums[0],min=nums[n-1];
for(int i=1;i<n;i++){
max=Math.max(max,nums[i]);
min=Math.min(min,nums[n-1-i]);
if(nums[i]<max)end=i;
if(nums[n-1-i]>min)start=n-1-i;
}
return end-start+1;
}
}
class Solution {
public int findUnsortedSubarray(int[] nums) {
int[] nn=nums.clone();
Arrays.sort(nn);
int l=nums.length,r=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=nn[i]){
l=Math.min(l,i);
r=Math.max(r,i);
}
}
return r-l<0?0:r-l+1;
}
}

Leeetcode--581. Shortest Unsorted Continuous Subarray的更多相关文章

  1. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

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

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

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

  3. 581. Shortest Unsorted Continuous Subarray

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

  4. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

  5. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...

  6. 【leetcode】581. Shortest Unsorted Continuous Subarray

    题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /** * @param {number[]} nums * @retur ...

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

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

  8. Shortest Unsorted Continuous Subarray LT581

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

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

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

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

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

随机推荐

  1. linux中 bashrc文件的alias添加快捷命令

    alias (为了简化命令操作,节省时间) 进入 /home下的用户,假设为 web 执行命令 ls -alh   找到 .bashrc 隐藏文件,如果没有则新建 通过  vi .bashrc  在里 ...

  2. 203. 阿里jetcache

    [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...

  3. 在C++程序中自动加入svn版本号

    原创文章,欢迎阅读,如果您想转载,请在第一行醒目注明原作者和原始链接. 为了方便追查和确认软件bug等问题,给软件或者库赋予版本号是个好办法. 最简单的版本号管理是记录编译时间: cout<&l ...

  4. IIC 设备使用

    通过 读 / 写 IIC 设备上特定的存储空间,来使用设备提供的功能: 存储空间地址 = 设备名 + 设备地址(Slave Address) + 寄存器地址 . 注:设备地址.寄存器地址.地址中写入数 ...

  5. CDH Spark-shell启动报错

    Spark-shell启动报错      具体报错如下: 在CDH  YARN 中修改以下两个配置: yarn.scheduler.maximum-allocation-mb 2048 yarn.no ...

  6. java学习笔记(十一):重写(Override)与重载(Overload)

    重写(Override) 重写是子类对父类的允许访问的方法的进行重新编写, 但是返回值和形参都不能改变. 实例 class Animal{ public void run(){ System.out. ...

  7. AFNetWorking 源码粗浅理解

    最近在看AFNetWorking的源码,整理出自己的一点思路.先从一个最简单的网络请求看: NSString *urlStr = [NSString stringWithFormat:@"h ...

  8. EasyUI datagrid单元格文本超出显示省略号,鼠标移动到单元格显示文本

    nowrap : true;  是前提 $('#×××').datagrid({ nowrap : true,//设置为true,当数据长度超出列宽时将会自动截取 }); 省略号样式: <sty ...

  9. linux下的音量控制器alsamixer 桌面v7

    转载 http://blog.sina.com.cn/s/blog_0ca103850102vpml.html 耳机 插后边 line out 耳机插前边 模拟耳机 声卡自带工具 linux下的音量控 ...

  10. maven插件后报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project

    在给eclipse换了高版本的maven插件后,引入jar包报如下的错误: org.apache.maven.archiver.MavenArchiver.getManifest(org.apache ...