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. iOS第三方登录qq

    http://blog.sina.com.cn/s/blog_7b9d64af0101e5vj.html

  2. eclipse创建maven组合项目

    创建普通maven项目作为父项目: packaging类型选择pom. 创建子项目即可(需要mvn eclipse:eclipse构建为eclipse项目)

  3. 防止get访问方式乱码

    有的情况下我们可能会用到get方式传参.就会涉及到乱码的问题... 现在就看一下如何解决get方式的乱码问题... 首先通过 javascript 的encodeURI()方法对参数进行两次编码. v ...

  4. spring 之 lookup-method & replace-method

    初始化bean的堆栈: at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy$Cglib ...

  5. 01.GOF设计模式_概述

    0.Abstract Fcatory 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类. 1. Adapter 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原 ...

  6. day31-软件开发规范

    一.为什么要规范软件开发? 1.1 为什么要有规范软件开发 你现在包括之前写的一些程序,所谓的'项目',都是在一个py文件下完成的,代码量撑死也就几百行,你认为没问题,挺好.但是真正的后端开发的项目, ...

  7. Java API下载和查阅方法

    使用来自API的类是简单的.只要把它当做自己写的就可以,采用import来引用,可以节省自己编程的气力~ 1.API文档下载地址 https://www.oracle.com/technetwork/ ...

  8. HTTPS好文推荐

    认真看完这几篇文章,HTTPS相关内容应该就能大概了解了. 1.https(ssl)协议以及wireshark抓包分析与解密 2.数字证书原理 3.也许,这样理解HTTPS更容易 4.SSL/TLS原 ...

  9. Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库

    Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库 unit Unit1; interface uses Winapi.Windows, Winapi.Mess ...

  10. 学习shell脚本之前的基础知识(一)(学习记录帖)

    记录命令历史:我们敲过的命令,linux会有记录,保存在家目录的.bash_history文件中.(备注:只有用户正常退出当前shell时,当前命令才会保存在.bash_history文件中) “  ...