[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 ...
随机推荐
- 使用nodejs+http(s)+events+cheerio+iconv-lite爬取2717网站图片数据到本地文件夹
源代码如下: //(node:9240) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' ...
- CentOS 7镜像下载
官网下载链接:http://isoredirect.centos.org/centos/7/isos/x86_64/ step1: 进入下载页,选择阿里云站点进行下载 Actual Country ...
- 通过sed -i ,通过准备好的sh脚本,来设置linux iptable白名单
通过准备好的sh脚本,来设置linux iptable白名单 特定字符串的行前插入新行 sed -i '/特定字符串/i 新行字符串' file #!/bin/bash del_stdin_buf() ...
- python中的捕获异常、异常跟踪
# 捕获异常,打印异常信息 try: 1/0 except Exception as e: print(e) 输出结果是integer division or modulo by zero,只知道是报 ...
- NazoHell 攻略
http://hell.one-story.cn/hell-start.html Level 0: http://nazohell.one-story.cn/nazohell-start.html 跳 ...
- Photoshop如何自定义形状
Photoshop如何自定义形状,自定义形状定义一次,可以随便使用,而且形状无大小,填充后不会有像素问题,普通人可把常用的自定义成形状,很方便.PS中有一些自定义的形状,自己可以随便使用,但是不是很全 ...
- [LeetCode] 675. Cut Off Trees for Golf Event 为高尔夫赛事砍树
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- teamviewer破解版
简介 TeamViewer是一个能在任何防火墙和NAT代理的后台用于远程控制的应用程序,桌面共享和文件传输的简单且快速的解决方案.为了连接到另一台计算机,只需要在两台计算机上同时运行 TeamView ...
- 轮胎魔术公式(Magic Fomula)模型
魔术公式是用三角函数的组合公式拟合轮胎试验数据,用一套形式相同的公式就可以完整地表达轮胎的纵向力Fx.侧向力Fy.回正力矩Mz.翻转力矩Mx.阻力矩My以及纵向力.侧向力的联合作用工况,故称为“魔术公 ...
- (一)线性表(linear list)
文章目录 定义 特点 ADT (abstract data type) 定义 摘抄自 维基百科 线性表(英语:Linear List)是由 n(n≥0)个 数据元素(结点)a[0],a[1],a[2] ...