原题链接在这里:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/

题目:

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:

  1. Then length of the input array is in range [1, 10,000].
  2. The input array may contain duplicates, so ascending order here means <=.

题解:

If the input nums is not sorted, then we want to find out the boundary index.

Then how, lets take start position as example. If the first part is sorted, then traversing right -> left, the updated minimum should be nums[i].

We want to find out the position when minimum != nums[i], and the most left one, then it should be start position of output.

Vice versa for the ending position.

Time Complexity: O(nums.length).

Space: O(1).

AC Java:

 class Solution {
public int findUnsortedSubarray(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
} int s = -1;
int e = -2;
int len = nums.length;
int max = nums[0];
int min = nums[len-1];
for(int i = 0; i<len; i++){
max = Math.max(max, nums[i]);
if(max != nums[i]){
e = i;
} min = Math.min(min, nums[len-1-i]);
if(min != nums[len-1-i]){
s = len-1-i;
}
} return e-s+1;
}
}

LeetCode Shortest Unsorted Continuous Subarray的更多相关文章

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

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

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

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

  3. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

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

  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. C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)

    问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...

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

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

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

    Given an integer array, you need to find one continuous subarray that if you only sort this 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. Leeetcode--581. Shortest Unsorted Continuous Subarray

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

随机推荐

  1. Yii2.0数据库查询实例(三)

    常用查询: // WHERE admin_id >= 10 LIMIT 0,10 User::find()->])->offset()->limit()->all() / ...

  2. Archimate

    archimate语言提供了一种用于表示企业体系结构的图形化语言,包括策略,转换和迁移规划,以及架构的动机和基本原理.该标准的设计尽可能紧凑,但仍可用于大多数企业体系结构建模需求.下图显示了Archi ...

  3. 吐槽 坑爹的MySQL安装路径选择

    一般再windows下面安装MySQL我们都会选择msi安装模式,然而安装最新版的MySQL(mysql-installer-community-5.7.11.0.msi 下载地址)发现MySQL默认 ...

  4. 【TopCoder】SRM160 DIV1总结

    做了两道题之后才发现做的是DIV1,不是DIV2,DIV1的第二道题是DIV1的第三道题,果断决定第3题就不看了=.= 250分题:给定一个时间起点8:00 AM DAY 1,再给出一组时间终点,格式 ...

  5. 2018-2019-2 20165114《网络对抗技术》Exp6 信息收集与漏洞扫描

    Exp6 信息收集与漏洞扫描 目录 一.实验目标与内容 二.实验后问题回答 三.实验过程记录 3.1 各种搜索技巧的应用 3.2 DNS IP注册信息的查询 3.3 基本的扫描技术 [主机发现] [端 ...

  6. CCNA 课程 一

    OSI 参考模型: 7应用层 6表示层 5会话层 4传输层   --  TCP / UDP (端口号) 3网络层   --  IP (原IP地址,目标IP地址) 2数据链路层  -- ARPA / e ...

  7. 【P1582】倒水(数论??暴力!!)

    这个题我很无语,一开始看绿题,还是数论,应该不会特别简单,应该要动笔写上好一会,过了一会旁边 #祝神 说这原来是个蓝题,我顿时觉得十分迷茫... 结果看了这个题看了一会,仔细一想,woc,这题怕不是可 ...

  8. Oracle数据库连接生成DataX的job-Json

    package com.bbkj.main; import com.bbkj.DbUtils.ConnectionPoolManager; import com.bbkj.DbUtils.DbUtil ...

  9. Django---model基础(单表)

    ORM 一.映射关系:           表名<--------------->类名           字段<-------------->属性          表记录& ...

  10. devstack apache2/keystone 没有启动

    在devstack中./rejoin-stack.sh 发现apache2/keystone 没有启动 单单手动启动apach2服务之后keystone并没有启动 sudo service apach ...