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

这个题目用sort去跟之前的nums比较, 然后把第一个不相等和最后一个不相等的index找到, return r - l + 1如果>1的话, 否则为0.

其次利用Stack, 把第一个不和谐的index找到, 再将nums reverse, 去找最后一个不和谐的index, return r - l + 1如果>1的话, 否则为0.

Code

1) Sort     T: O(nlgn)    S: O(n)

class Solution:
def findShortestSubarry(self, nums):
nums_copy, l, r, lr = sorted(nums), len(nums)-1, 0, len(nums)
for i in range(lr):
if nums[i] != nums_copy[i]:
l = min(l, i)
r = max(r, i)
ans = r-l + 1
return ans if ans > 0 else 0

2. Stack   T: O(n)    S; O(n)

class Solution:
def findShortestSubarry(self, nums):
stack, l, r, lr = [], len(nums)-1, 0, len(nums)
for i in range(lr):
while stack and nums[stack[-1]] > nums[i]:
l = min(l, stack.pop())
stack.append(i)
stack = []
for i in range(lr)[::-1]:
while stack and nums[stack[-1]] < nums[i]:
r = max(r, stack.pop())
stack.append(i)
ans = r - l + 1
return ans if ans > 0 else 0

[LeetCode] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack的更多相关文章

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

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

  2. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

    problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...

  3. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

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

  4. 【leetcode】581. Shortest Unsorted Continuous Subarray

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

  5. 581. Shortest Unsorted Continuous Subarray

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

  6. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

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

    581. 最短无序连续子数组 581. Shortest Unsorted Continuous 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. LeetCode Shortest Unsorted Continuous Subarray

    原题链接在这里:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/ 题目: Given a ...

随机推荐

  1. 数据库高分笔记01:事务ACID

    作者:arccosxy  转载请注明出处:http://www.cnblogs.com/arccosxy/ 事务就是一组原子性的SQL查询,或者说一个独立的工作单元.如果数据库引擎能够成功地对数据库应 ...

  2. maven安装与创建多模块项目

    最新版已同步至 http://yywang.info/2014/05/31/maven-install-and-create-project/ maven是一个比较流行的项目管理工具,在最近参与的项目 ...

  3. 将音乐生成波浪图形,JavaScript Html5

    x 省略废话(N+)... Windows Media Palyer中的经典波浪形 自己也行动手做一个,最好是JavaScript实现的, 搜索到了资源部分关键词"HTML5 频谱" ...

  4. ORM 简介 单表操作

    cls超 Django基础五之django模型层(一)单表操作 本节目录 一 ORM简介 二 单表操作 三xxx 一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型 ...

  5. Chrome浏览器如何调试移动端网页信息

    Chrome浏览器如何调试移动端网页信息 2017年08月12日 12:42:20 阅读数:835 最近在弄项目,用WebView加载一个页面,想追踪页面中一个按钮的点击事件.这个可能就需要调试这个页 ...

  6. 抽屉之Tornado实战(7)--form表单验证

    在这里,我们把form表单验证的代码进行工具化了,以后稍微修改一下参数就可以拿来用了 先贴上代码 forms.py from backend.form import fields class Base ...

  7. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  8. WIN10登录时找不到Administrator用户

    前提:WIN才安装的系统登录时只看到admin用户看不到administrator用户 1. 按网上方法,进入[此电脑]--[管理]--[系统工具]--[本地用户和组]--[用户] 2. 双击打开Ad ...

  9. json 脚本入库的几种方法

    json 脚本入库的几种方法,见代码: #-*- encoding: utf-8 -*- #第一种mongodb入库 # from pymongo import * # import json # c ...

  10. jquery图片懒加载效果

    1.要引入jquery 2.要引入underscore.js <!DOCTYPE html> <html lang="en"> <head> & ...