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) {
if (nums == null || nums.length == || nums.length == ) return ;
int max = Integer.MIN_VALUE, end = -;
// iterate from beginning of array find the last element which is smaller than
// the last seen max from its left side and mark it as end
for (int i = ; i < nums.length; i++) {
max = Math.max(max, nums[i]);
if (nums[i] < max)
end = i;
}
if (end == -) return ; int min = Integer.MAX_VALUE, begin = -;
for (int i = nums.length - ; i >= ; i--) {
min = Math.min(min, nums[i]);
if (nums[i] > min)
begin = i;
}
return end - begin + ;
}
}

Shortest Unsorted Continuous Subarray的更多相关文章

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

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

  2. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

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

  3. Shortest Unsorted Continuous Subarray LT581

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

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

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

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

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

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

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

  7. Leeetcode--581. Shortest Unsorted Continuous Subarray

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

  8. 581. Shortest Unsorted Continuous Subarray

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

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

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

  10. LeetCode581. Shortest Unsorted Continuous Subarray

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

随机推荐

  1. mouseleave([[data],fn])

    mouseleave([[data],fn]) 概述 当鼠标指针离开元素时,会发生 mouseleave 事件.该事件大多数时候会与mouseenter 事件一起使用. 与 mouseout 事件不同 ...

  2. sql server 游标的知识

    一:认识游标   游标是SQL Server的一种数据访问机制,它允许用户访问单独的数据行.用户可以对每一行进行单独的处理,从而降低系统开销和潜在的阻隔情况,用户也可以使用这些数据生成的SQL代码并立 ...

  3. Shell 脚本语法

    条件测试:test  和  [ 命令 test 或 [ 可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1(注意与C ...

  4. 修改quartus 配置rom时memory很小的问题。

    我用的是quartus ii 13版本的仿真软件,在做VGA实验时显示用到640*480的图片所以就需要307200*1bit的rom.但是坑爹的megawizard- plug-in-manager ...

  5. redhat7.4安装git(按照官网从源码安装)

    按照官方文档建议使用源码安装 1.为什么不用yum安装 yum安装确实简单,只用一行命令就可以了,但是yum安装的版本太低. //安装前使用info查看git版本信息等 yum info git yu ...

  6. Spring boot 配置 Tomcat 临时文件缓存目录

    1. 问题现象 spring boot 项目中,Tomcat 接收到 content-type 为 multipart/form-data 的请求时,需要将接收的文件缓存到临时目录(默认下载 /tmp ...

  7. MISS YOU

      文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  8. (八)爬虫之js调试(登陆知乎)

    上次爬取网易云音乐,折腾js调试了好久,难受....今天继续练练手,研究下知乎登陆,让痛苦更猛烈些. 1.简单分析 很容易就发现登陆的url=“https://www.zhihu.com/api/v3 ...

  9. embeddable persistent key-value store for fast storage

    A persistent key-value store for fast storage environmentsRocksDB is an embeddable persistent key-va ...

  10. mysql密码设置为空怎么办?

    由于很多童鞋安装使用MySQL时,安装时没有设置密码,或者像我一样图省事设置密码为空,想为其设置新密码: 1.点击  开始------>运行----在弹出的对话框中输入cmd 如下图: 2.使用 ...