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 <=.

最短无序连续子数组

基本就是排序生成新数组,与原数组进行比较,找出左右两端两数组索引相同时但数据不同的索引.

class Solution(object):
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums2 = sorted(nums)
if nums == nums2:
return 0
i = 0
while nums[i] == nums2[i]:
i += 1
j = -1
while nums[j] == nums2[j]:
j -= 1
l = len(nums[i:j])+1
return l

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 su ...

  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. Shortest Unsorted Continuous Subarray LT581

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

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

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

  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. CMD定时倒数

    修改if %count%==20 goto finish改变秒数 CMD: mode con: cols=80 lines=25color 4ftitle Please WaitSET /P var= ...

  2. [多线程]线程基础(对象锁、class锁、同步、异步)

    synchronized.volatile.ReentrantLock.concurrent 线程安全:当多个线程访问某一个类(对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法) ...

  3. Maven父工程

    1.创建Maven工程时 Packaging 选项为 pom 2.父工程目录结构只有pom.xml(父工程不进行编码) 3.需要的依赖信息在父工程中定义,子模块继承 5.将各个子模块聚合到一起 6.将 ...

  4. TP的di

    依赖注入的意思是通过反射分析类所依赖的其他类,从容器中获取相应的对象并自动注入到类里面 首先依赖注入和控制反转说的是同一个东西,是一种设计模式,这种设计模式用来减少程序间的耦合,鄙人学习了一下,看TP ...

  5. Linux中的Wheel组的作用

    原文:http://www.360doc.com/content/11/0505/10/4644186_114496525.shtml Linux中的Wheel组的作用(用自己的话翻译的) (原文) ...

  6. delphi 动态绑定代码都某个控件

    delphi 动态绑定代码都某个控件 http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Rtti.TRttiType_(Delphi)Butt ...

  7. 机器学习进阶-图像形态学操作-梯度运算 cv2.GRADIENT(梯度运算-膨胀图像-腐蚀后的图像)

    1.op = cv2.GRADIENT 用于梯度运算-膨胀图像-腐蚀后的图像 梯度运算:表示的是将膨胀以后的图像 - 腐蚀后的图像,获得了最终的边缘轮廓 代码: 第一步:读取pie图片 第二步:进行腐 ...

  8. ---github git clone 加速

    https://www.zhihu.com/question/27159393/answer/35528173 git config --global http.postBuffer 52428800 ...

  9. jsfl 读取xml

    var fileURI = "file:///c|/temp/mydata.txt"; var dataXml = new XML(FLfile.read(fileURI)); v ...

  10. Linux查看当前系统的发行版信息

    lsb_release命令用来查看当前系统的发行版信息(prints certain LSB (Linux Standard Base) and Distribution information.). ...