581. Shortest Unsorted Continuous Subarray
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:
- Then length of the input array is in range [1, 10,000].
- 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的更多相关文章
- 【leetcode_easy】581. Shortest Unsorted Continuous Subarray
problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况
[抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- 【leetcode】581. Shortest Unsorted Continuous Subarray
题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /** * @param {number[]} nums * @retur ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- Shortest Unsorted Continuous Subarray LT581
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
随机推荐
- python3基础:基本语句(2017)
http://www.cnblogs.com/qq21270/p/4591318.html 字符串.文本文件 http://www.cnblogs.com/qq21270/p/7872824.htm ...
- Retrofit添加自定义转换器
Retrofit2开始支持多种 Converter 并存,在之前,如果你遇到这种情况:一个 API 请求返回的结果需要通过 JSON 反序列化,另一个 API 请求需要通过 proto 反序列化,唯一 ...
- 揭开牙病之谜 与牙医说再见<转>
原贴地址:https://www.douban.com/group/topic/44383918/ -------------------------------------------------- ...
- classloader trace
类加载机制: 程序启动时,根据入口函数调用相关功能,功能在不同类中即在不同的class文件中,jvm根据类加载机制来动态加载class文件到内存中,只有被加载后才能被调用,否则引发异常 1.装载:查找 ...
- js判断对象
一般学java的小伙伴,刚开始写js时如果遇到要判断一个字符串是否不为空,往往会这样写 if(str != undefined && str != null && st ...
- Redis进阶实践之二如何在Linux系统上安装安装Redis(转载)(2)
Redis进阶实践之二如何在Linux系统上安装安装Redis 一.引言 上一篇文章写了“如何安装VMware Pro虚拟机”和在虚拟机上安装Linux操作系统.那是第一步,有了Linux操作系统,我 ...
- 转: python requests的安装与简单运用
requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...
- shell流程控制与循环结构
shell脚本中的流程控制有if/else语句.case语句,循环结果包括for循环.while循环.until循环等内容. if语句 (1)最简单的if语句.使用格式有2种方式,分别如下 使用格式1 ...
- Linux find命令使用方法
Linux中find命令用来在指定目录下查找文件.通过组合不同参数可以在linux系统中快速查找需要的文件或目录. find命令语法 格式:find pathname -options [ -pr ...
- 什么是socket?
1.七层协议简化为四层:应用层.传输层.网络层.链路层:2.套接字是应用层和TCP/IP协议族通信间的软件抽象层,将TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用实现进程在网络中的通信:本地 ...