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. 思路:
第一遍从左往右扫,看当前值是否比找到的最大值还小,如果是这样的话,明显中间不是递增的。同样,从右往左扫,如果当前值比已经找到的最小值大,那么中间部分也不是递增的。
class Solution {
public int findUnsortedSubarray(int[] nums) {
if (nums == null || nums.length == || nums.length == ) return ;
int max = Integer.MIN_VALUE, end = -;
// iterate from beginning of array find the last element which is smaller than
// the last seen max from its left side and mark it as end
for (int i = ; i < nums.length; i++) {
max = Math.max(max, nums[i]);
if (nums[i] < max)
end = i;
}
if (end == -) return ; int min = Integer.MAX_VALUE, begin = -;
for (int i = nums.length - ; i >= ; i--) {
min = Math.min(min, nums[i]);
if (nums[i] > min)
begin = i;
}
return end - begin + ;
}
}
Shortest Unsorted Continuous 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: 使用一个辅助数组,新建一个跟原数组一模一 ...
- Shortest Unsorted Continuous Subarray LT581
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
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 ...
- Leeetcode--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 subarr ...
- 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况
[抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...
- LeetCode581. Shortest Unsorted Continuous Subarray
Description Given an integer array, you need to find one continuous subarray that if you only sort t ...
随机推荐
- leetcode解题报告(10):Merge Two Sorted Lists
描述 Merge two sorted linked lists and return it as a new list. > The new list should be made by sp ...
- 【转】java 解析多层json
java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...
- 百度地图API根据地名获取经纬度
运用了Geocoding API,它包括地址解析和逆地址解析功能. 地址解析是指,由详细到街道的结构化地址得到百度经纬度信息,且支持名胜古迹.标志性建筑名称直接解析返回百度经纬度.例如:“北京市海淀区 ...
- C++常用字符串函数使用整理
strlen(字符数组) 功能:求字符串长度. 说明:该函数的实参可以是字符数组名,也可以是字符串. 使用样例: char s1[80] = "China"; cout<&l ...
- Hdu5178
Hdu5178 题意: 题目给你N个点,问有多少对点的长度小于K . 解法: 首先将所给的坐标从大到小排序,则此题转化为:对排序后的新数列,对每个左边的\(x_a\)找到它右边最远的 $ x_b $ ...
- Apache Flink - 基本API概念
Flink程序是实现分布式集合转换的常规程序.集合最初是从源创建的.通过接收器(slink)返回结果,接收器可以将数据写到某个文件或stdout.Flink可以在各种环境(context)中运行,本地 ...
- 笔记七(编写第一个UEFI程序)
搭建好uefi开发环境之后,在MyWorkspace文件夹中建立一个文件夹ExamplePkg; ,然后在ExamplePkg文件夹中创建HelloWorld文件夹,Include文件夹,Exampl ...
- springboot和hadoop2.7.7集成开发
1.本人在腾讯云安装hadoop2.7.7,详细安装请看以前的博客 2.pom.xml文件 <?xml version="1.0" encoding="UTF-8& ...
- 003-多线程-JUC线程池-几种特殊的ThreadPoolExecutor【newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor、newScheduledThreadPool】
一.概述 在java doc中,并不提倡我们直接使用ThreadPoolExecutor,而是使用Executors类中提供的几个静态方法来创建线程池: 以下方法是Executors下的静态方法,Ex ...
- [maven]scope之test
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...