原题链接在这里: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. Oracle表与约束关系

    手动回收表的存储方式: SQL> alter table aux_emp deallocate unused; //回收所有未使用的存储空间 表已更改. 回收aux_emp的存储空间,保留50K ...

  2. window端口号被占用解决

    1. 找到占用该端口的pid netstat -ano|findstr "端口号" 2. 强制关闭该占用该端口的进程 // 关闭 taskkill -pid 刚才查的pid // ...

  3. Java Map增删改查

    示例代码: 学生类 package com.imooc.collection; import java.util.HashSet; import java.util.Set; /** * 学生类 * ...

  4. RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)

    . I have added two obejcts data source to Report Viewer. 2. in RDLC i have created two tables and in ...

  5. 修改和重置WAMP的phpMyAdmin密码

    WAMP是Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,常用来搭建动态网站或者服务器的开源软件. 1.下载页面:http://www.wampserver ...

  6. How to choose from Viewstate, SessionState, Cookies and Cache

    https://devshop.wordpress.com/2008/04/10/how-to-choose-from-viewstate-sessionstate-cookies-and-cache ...

  7. 【bzoj1899】[Zjoi2004]Lunch 午餐(贪心+dp)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1899 显然为了节省时间,吃饭慢的人要先打饭.于是我们可以先把所有人按吃饭时间排序,于是 ...

  8. java中,return和return null有什么区别吗?

    java中,return和return null有什么区别吗? 最大的区别:return;方法的返回值必须是void!return null;方法的返回值必须不是 原始数据类型(封装类除过)和void ...

  9. MAC 系列 之XCode7.1 + HBuilder MUI 离线打包 ipa 上次application leader 问题:ERROR ITMS - 90534

    解决方法:这个原因 网上说法是 beta 测试版本:不过的确是beta版本(7.3 beta)打包的,所以我有下载了一个正式版本 7.1版本. 再次进行测试打包!

  10. vue 列表渲染 v-for循环

    v-for循环指令类似与html中C标签的循环,同样可以遍历数组,集合. 1.这里演示一下遍历数组的基本用法,代码如下 <!DOCTYPE html> <html> <h ...