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. learning express step(十一)

    learning express.Router() code: const express = require('express'); const app = express(); var route ...

  2. pandas常用操作命令大全

    网上的有个别不对 实际敲了一下  有补充了点常用的环境IDE anaconda  python3.7 在这个速查手册中,我们使用如下缩写: df:任意的Pandas DataFrame对象 s:任意的 ...

  3. linux查看服务安装目录redis

    如果用命令 which redis 或者 whereis redis 都找不到安装目录, 可使用以下办法: ps -aux | grep redis  或者ps -ef|grep redis 假如得到 ...

  4. CISCO实验记录六:EIGRP路由协议

    一.要求 1.查看当前路由协议 2.清空路由设置 3.使用EIGRP协议创建路由 4.查看EIGRP的邻居表 5.关闭自动汇总 6.使用手工汇总 二.实现 1.查看当前路由协议 #show ip pr ...

  5. 记录Redis连接未正确释放,TCP连接过多,造成服务器上部分功能不可用和linux服务器内存一直增加问题

    问题1 多人共享开发服务器(windows系统),我们小组有个程序,定时检测mongodb,redis,mysql连接是否正常,程序启动一段时间后,服务器管理人员找到我们说,我们的某个pid的程序把T ...

  6. (main)贝叶斯统计 | 贝叶斯定理 | 贝叶斯推断 | 贝叶斯线性回归 | Bayes' Theorem

    2019年08月31日更新 看了一篇发在NM上的文章才又明白了贝叶斯方法的重要性和普适性,结合目前最火的DL,会有意想不到的结果. 目前一些最直觉性的理解: 概率的核心就是可能性空间一定,三体世界不会 ...

  7. Python 类中__init__()方法中的形参与如何修改类中属性的值

    一.__init__()方法 如果__init__()方法为 class Cat(): def __init__(self,num) : self.num=num Python中类的__init__( ...

  8. embeddable persistent key-value store for fast storage

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

  9. Java静态方法和实例方法区别详解

    静态方法和实例方法的区别主要体现在两个方面: 在外部调用静态方法时,可以使用"类名.方法名"的方式,也可以使用"对象名.方法名"的方式.而实例方法只有后面这种方 ...

  10. test20190510