[LeetCode] 581. 最短无序连续子数组 ☆
描述
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
你找到的子数组应是最短的,请输出它的长度。
示例 1:
输入: [2, 6, 4, 8, 10, 9, 15]
输出: 5
解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
说明 :
输入的数组长度范围在 [1, 10,000]。
输入的数组可能包含重复元素 ,所以升序的意思是<=。
解析
排序
先排序,再比较左、右的第一个不一致的index即可。
巧思
看代码即可。
代码
//[2, 6, 4, 8, 10, 9, 15]
public int findUnsortedSubarray(int[] nums) {
if (null == nums || nums.length <= 0) {
return 0;
}
int min = Integer.MAX_VALUE;//最小的一个:后一个数大于前一个数的 从左往右遍历. 即上面的4
int max = Integer.MIN_VALUE;//最小的一个:前一个数大于后一个数的 从右往左遍历. 即上面的10
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
min = Math.min(min, nums[i]);
}
}
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1]) {
max = Math.max(max, nums[i]);
}
}
int left = 0, right = 0;
//找到第一个大于min的值,即为最左边的应该排序的值
for (int i = 0; i < nums.length; i++) {
if (nums[i] > min) {
left = i;
break;
}
}
//找到第一个小于max的值,即为最右边的应该排序的值
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] < max) {
right = i;
break;
}
}
return right - left <= 0 ? 0 : right - left + 1;
}
[LeetCode] 581. 最短无序连续子数组 ☆的更多相关文章
- Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)
581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...
- Leetcode 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous 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 ...
- leetcode面试题42. 连续子数组的最大和
总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目 面试题42. 连续子数 ...
- C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)
问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
随机推荐
- 安装比特币区块链钱包API(Blockchain Wallet用户发送和接收比特币的简单API)
区块链钱包API提供了一个简单的界面,商家可以用它以编程方式与钱包进行交互. 安装:要使用此API,您需要运行负责管理区块链钱包的小型本地服务. 您的应用程序通过HTTP API调用在本地与此服务进行 ...
- centos7.6环境编译安装php-7.2.24修复最新 CVE-2019-11043 漏洞
先编译安装php-7.2.24,然后编译安装扩展 主版本地址地址:https://www.php.net/distributions/php-7.2.24.tar.gz # 编译 php-7.2.24 ...
- LeetCode 993. Cousins in Binary Tree(判断结点是否为Cousin)
993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...
- 基因型数据正负链怎么翻转(snp flip)
在合并数据过程当中,经常会发现不同来源的数据正负链不是统一的,这是一件很头疼的事. 正负链没有统一的情况下直接合并在一起会产生什么后果呢. 举个最简单的例子,假如我们从小明和小红分别拿到了一批基因型数 ...
- js 类型判断
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 多核vs多处理器
多核vs多处理器 多核CPU性能最好,但成本最高:多CPU成本小,便宜,但性能相对较差 线程数=cpu处理器个数 * 一个cpu内的核数[如果有超线程,再乘以超线程数] 多核 CPU 和多个 CPU ...
- Django文档阅读之查询
创建对象 为了在Python对象中表示数据库表数据,Django使用直观的系统:模型类表示数据库表,该类的实例表示数据库表中的特定记录. 要创建对象,请使用模型类的关键字参数对其进行实例化,然后调用s ...
- 第3/7Beta冲刺
1.团队成员 成员姓名 成员学号 秦裕航 201731062432(组长) 刘东 201731062227 张旭 201731062129 王伟 201731062214 2.SCRU部分 2.1各成 ...
- lay-verify
lay-verify:是表单验证的关键字 required (必填项) phone(手机号) email(邮箱) url(网址) number(数字) date(日期) identity(身份证) 自 ...