LeetCode Shortest Unsorted Continuous Subarray
原题链接在这里:https://leetcode.com/problems/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 <=.
题解:
If the input nums is not sorted, then we want to find out the boundary index.
Then how, lets take start position as example. If the first part is sorted, then traversing right -> left, the updated minimum should be nums[i].
We want to find out the position when minimum != nums[i], and the most left one, then it should be start position of output.
Vice versa for the ending position.
Time Complexity: O(nums.length).
Space: O(1).
AC Java:
class Solution {
public int findUnsortedSubarray(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int s = -1;
int e = -2;
int len = nums.length;
int max = nums[0];
int min = nums[len-1];
for(int i = 0; i<len; i++){
max = Math.max(max, nums[i]);
if(max != nums[i]){
e = i;
}
min = Math.min(min, nums[len-1-i]);
if(min != nums[len-1-i]){
s = len-1-i;
}
}
return e-s+1;
}
}
LeetCode Shortest Unsorted Continuous Subarray的更多相关文章
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 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 ...
- C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)
问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- Shortest Unsorted Continuous Subarray LT581
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- Leeetcode--581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
随机推荐
- 建议44:理解模块pickle优劣
# -*- coding:utf-8 -*- ''' pickle 估计是最通用的序列化模块了,它还有个C 语言的实现cPickle,相比pickle 来说 具有较好的性能,其速度大概是pickle ...
- 试坑不完美的 clip-path (我说的 CSS 的那个)
需求跟我说,咱们要创新,想做一个蜂巢状的列表,年少无知的我竟然一口答应了,全然因为刚接触了 clip-path: But,然而,不幸的是,这只是坎坷路途的开始.... clip-path 的教程很多了 ...
- [SCOI2005]超级格雷码
题目 BZOJ 洛谷 做法 爆搜真有意思 满足不重复且异或后仅一位为\(1\) 利用奇偶性交叉搜索(仅改变一位) My complete code #include<bits/stdc++.h& ...
- iOS_多线程(二)
上篇中我们分享了NSThread.NSOperation&NSOperationQueue如何实现多线程,今天我们来看下第三种实现多线程的方式:GCD(Grand Central Dispat ...
- strspn() 和 strcspn() 函数【转】
本文转载自:https://flyer103.wordpress.com/2011/06/03/strspn-%E5%92%8C-strcspn-%E5%87%BD%E6%95%B0/ 前几天在看一本 ...
- C# 处理base64 以及base64的原理分析
base64的原理, http://www.cnblogs.com/diligenceday/p/6002382.html http://www.cnblogs.com/chengxiaohui/ar ...
- BZOJ-1396: 识别子串
后缀自动机+线段树 先建出\(sam\),统计一遍每个点的\(right\)集合大小\(siz\),对于\(siz=1\)的点\(x\),他所代表的子串只会出现一次,设\(y=fa[x]\),则这个点 ...
- mongodb 的安装(Centor OS )
1.下载地址 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.2.tgz 2.解压.配置 tar zxvf mongodb ...
- java arrays类学习
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的. 具有以下功能: (1)给数组赋值:通过fill方法. (2)对数组排序:通过sort方法,按升序. (3)比较数组:通 ...
- Neutron新进展|DragonFlow在Mitaka版本中的Roadmap
OpenStack网络在Mitaka版本中将有哪些新变化?1月11日到12日,DragonFlow的PTL——Eran Gampel,Kuryr的PTL——Gal Sagie,和他们的老大从以色列来到 ...