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 ...
随机推荐
- Jvm的体系结构
1.垃圾回收器 垃圾回收器(又称为gc):是负责回收内存中无用的对象(好像地球人都知道),就是这些对象没有任何引用了,它就会被视为:垃圾,也就被干掉了. 2.类装载子系统 一听名字,大家就知道,肯定是 ...
- Vim 命令、操作、快捷键
打开单个文件:vim file 同时打开多个文件:vim file1 file2 file3 ... 在vim窗口中打开一个新文件 : :open file 在新窗口中打开文件: :split fi ...
- leetcode78
本题是回溯法的基本应用,深度优先遍历,使用递归实现. class Solution { public: ]; vector<vector<int>> R; int n; //t ...
- EasyARM-iMX283A的U盘使用教程
在编写代码前我们先来EasyARM-iMX283A对U盘使用的操作. 我们先拿一个U盘进行格式化 在U盘中写一些文件保存后,弹出U盘. 将U盘插入EasyARM-iMX283A的开发板. [注意]Ea ...
- [phvia/firman] PHP多进程服务器模型中的惊群
[ 典型场景 ] 典型的多进程服务器模型是这样的,主进程绑定ip,监听port,fork几个子进程,子进程安装信号处理器,随后轮询资源描述符检查是否可读可写: 子进程的轮询又涉及到 IO复用,acce ...
- linux下用python搭建简单的httpServer
1.服务器端:python -m SimpleHTTPServer 12000 python -m : 相当于import,当做模块来启动; 后面的12000代表的是端口 使用浏览器打开如下: 2. ...
- uboot1.1.6
http://blog.csdn.net/lizuobin2/article/details/52061530
- zabbix钉钉报警
我们在钉钉上建立群聊,然后在群聊上添加钉钉机器人: 编写,脚本需要放在zabbix 的alertscripts目录下(如果不知道该目录的位置,可以使用find命令查找) find / -iname a ...
- swift 需求: 导航栏和HeaderView 使用一个背景图片。
问题界面 需求: 导航栏和HeaderView 使用一个背景图片.解决方案: 让 导航栏 变成透明. override func viewWillAppear(_ animated: Bool) { ...
- SecureCRT问题
使用SecureCRT 与虚拟机进行通信,提示The remote system refused the connection 解决:由于缺少SSH服务器端 sudo apt-get install ...