[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

题目有歧义:其实没有“最短”的概念,找到一个范围就行了

[奇葩corner case]:

1234,输出0。因此i小j大的初始值是-1,0。别的地方不知道能否试试?

[思维问题]:

指针对撞一直走,但是没想到最后会ij颠倒大小。

[一句话思路]:

i小j大变成了i大j小,所以结果是i - j + 1

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 不可能i j,l r两对指针同时走的,一对就够了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

指针对撞一直走,但是没想到最后会ij颠倒大小。i - j + 1

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int findUnsortedSubarray(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return 0;
} //ini: l r
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, i = -1, j = 0; //for loop
for (int l = 0, r = nums.length - 1; r >= 0; l++, r--) {
max = Math.max(nums[l], max);
if (nums[l] != max) {
i = l;
} min = Math.min(nums[r], min);
if (nums[r] != min) {
j = r;
}
} return i - j + 1;
}
}

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. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

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

  5. 【leetcode】581. Shortest Unsorted Continuous Subarray

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

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

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

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

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

  8. C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)

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

  9. [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. git 生成公钥与私钥

    一. 第1步:创建SSH Key.在windows下查看[c盘->用户->自己的用户名->.ssh]下是否有id_rsa.id_rsa.pub文件,如果没有需要手动生成.打开git ...

  2. 使用IntelliJ IDEA开发SpringMVC网站的学习

    最近开始了“使用IntelliJ IDEA开发SpringMVC网站”的学习,有幸看到一份非常完善的学习资料,笔者非常用心的详细注释了一份关于博客的开发过程和细节,并且在评论中回复大家提出的问题,非常 ...

  3. 解决direct2d拖拽窗口闪烁

    响应WM_ERASEBKGND,在OnEraseBkgnd()处返回FALSE,阻止GDI重绘客户区背景色,设置背景色的工作交给Direct2D在Render时设置,否则在Resize时会出现窗口闪烁 ...

  4. 记一次愚蠢的gradle操作

    今晚把工作移植到mac平台,在用gradle命令 exec ./gradlew --parallel --info assembleDebug 打包apk时卡住,gradle一直处于下载状态,过了几分 ...

  5. var ev = ev || event

    event是事件对象(也是window的属性),但不是标准的,只有IE支持. 在W3C标准支持的浏览器下事件对象是引发事件函数的第一个参数,参数名随意. 所以,我们一般使用事件对象: function ...

  6. 安装并配置工具以使用iOS进行构建

    Visual Studio 2015   Visual Studio文档的新家是docs.microsoft.com上的Visual Studio 2017文档 . 有关Visual Studio 2 ...

  7. apache 2 修改虚拟目录

    准备好环境,就要开始进行开发了.这一篇,我们在Ubuntu Apache上配置虚拟目录. 知识准备: 区别于Windows 下apache,配置文件通常只有一个,就是httpd.conf. Linux ...

  8. HTML简历

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. explain解析

    MySQL执行计划调用方式执行计划包含的信息执行计划显示内容解读MySQL执行计划的局限MySQL5.6支持OPTIMIZER_TRACE 1.什么是归并排序?将已有序的子序列合并,得到完全有序的序列 ...

  10. MFC学习(三)程序入口和执行流程

    1) WIN32 API程序当中,程序入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用程序窗口函数.而在MFC程序当中我们 ...