Leeetcode--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.
class Solution {
public int findUnsortedSubarray(int[] nums) {
int n=nums.length,end=-2,start=-1,max=nums[0],min=nums[n-1];
for(int i=1;i<n;i++){
max=Math.max(max,nums[i]);
min=Math.min(min,nums[n-1-i]);
if(nums[i]<max)end=i;
if(nums[n-1-i]>min)start=n-1-i;
}
return end-start+1;
}
}
class Solution {
public int findUnsortedSubarray(int[] nums) {
int[] nn=nums.clone();
Arrays.sort(nn);
int l=nums.length,r=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=nn[i]){
l=Math.min(l,i);
r=Math.max(r,i);
}
}
return r-l<0?0:r-l+1;
}
}
Leeetcode--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 subarr ...
- 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 ...
随机推荐
- 【Linux】【Selenium】安装Chrome和ChromeDriver的配置
转自:https://www.cnblogs.com/longronglang/p/8078898.html 1.安装chrome sudo apt-get install libxss1 libap ...
- xxx.jar 中没有主清单属性
springboot 中是可以通过 jar 将整个项目打包成一个fat jar 的, 这个大家都知道. <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 --&g ...
- ViewModel学习
ViewModel是一个负责准备和管理Activity或Fragment数据的类.它还处理Activity / Fragment与应用程序其余部分的通信(例如,调用业务逻辑类). 始终与范围(Frag ...
- mysql实用函数
1. group_concat(); 可以将选择的字段列数据,分组以逗号分隔成一串.实用方便.select id,group_concat(distinct name) from ttt group ...
- Python3 复制和深浅copy
赋值: 列表的赋值: list1 = ['peter','sam'] list2 = list1 print(list1,id(list1)) print(list2,id(list2)) list1 ...
- setting设置
1.调试 DEBUG = True 2.设置template TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTem ...
- win10 下安装 neo4j(转)
1.neo4j介绍 neo4j是基于Java语言编写图形数据库.图是一组节点和连接这些节点的关系.图形数据库也被称为图形数据库管理系统或GDBMS.详细介绍可看Neo4j 教程 2.安装Java jd ...
- javaMail实现收发邮件(三)
JavaMail API中定义了一个java.mail.Transport类,它专门用于执行邮件发送任务,这个类的实例对象封装了某种邮件发送协议的底层实施细节,应用程序调用这个类中的方法就可以把Mes ...
- Java虚拟机 内存区域划分
(图片来自https://www.cnblogs.com/whgk/p/6138522.html) 先从线程私有区开始介绍 虚拟机栈 Java虚拟机栈是由一个个栈帧组成的,当一个方法被调用时,代表这个 ...
- as3.0画直线
import flash.display.Shape; import flash.events.MouseEvent; import flash.geom.Point; var line:Shape; ...