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. SimpleDateFormat线程不安全

    http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html dateUtil替换

  2. 重识linux-linux的新增与删除用户组和切换命令

    重识linux-linux的新增与删除用户组 1 相关文件 /etc/group /etc/gshadow 2操作相关 groupadd group1 groupmod group1 groupdel ...

  3. 《算法》第三章部分程序 part 1

    ▶ 书中第三章部分程序,加上自己补充的代码,包括单词频率统计,(单链表)顺序查找表,二分查找表 ● 单词频率统计 package package01; import edu.princeton.cs. ...

  4. python中的lstrip、rstrip、strip

    lstrip()移除左侧空白符 rstrip()移除右侧空白符 strip()移除两边的空白符 1 a = " hello world" 2 a1 = a.lstrip()3 pr ...

  5. 如何在github上创建仓库,并将本地的文件上传到对应的远程仓库

    1.安装git,可从 http://www.bootcss.com/p/git-guide/下载git 2.在github上创建仓库,注意不勾选Initialize this repository w ...

  6. Ubuntu系统安装

    一:Ubuntu14.04 安装及使用:[1]制作安装U盘(选择USB-HDD+”并单击后,           一定要点“写入”,注意,“写入”是一个按钮) 以下是详细的连接步骤: https:// ...

  7. ubuntu 系统分区

    参考:https://blog.csdn.net/kudou1994/article/details/80913059

  8. shell执行class或jar

    mc11>java -cp /home/ap/user/webproject/web.war/WEB-INF/lib/*:. com.userpackage.ExcelDemo 说明:/home ...

  9. 彻底关闭Windows Defender丨Win10

    关闭Windows Defender Win10正式版怎么关闭windows defender 首先关闭windows defender,因重启电脑后win10 会自动重启defender,所以需要禁 ...

  10. 机器学习进阶-目标跟踪-KCF目标跟踪方法 1.cv2.multiTracker_create(构造选框集合) 2. cv2.TrackerKCF_create(获得KCF追踪器) 3. cv2.resize(变化图像大小) 4.cv2.selectROI(在图像上框出选框)

    1. tracker = cv2.multiTracker_create() 获得追踪的初始化结果 2.cv2.TrackerKCF_create() 获得KCF追踪器 3.cv2.resize(fr ...