LeetCode Shortest Unsorted Continuous Subarray
原题链接在这里:https://leetcode.com/problems/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.
Note:
- Then length of the input array is in range [1, 10,000].
- The input array may contain duplicates, so ascending order here means <=.
题解:
If the input nums is not sorted, then we want to find out the boundary index.
Then how, lets take start position as example. If the first part is sorted, then traversing right -> left, the updated minimum should be nums[i].
We want to find out the position when minimum != nums[i], and the most left one, then it should be start position of output.
Vice versa for the ending position.
Time Complexity: O(nums.length).
Space: O(1).
AC Java:
class Solution {
public int findUnsortedSubarray(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int s = -1;
int e = -2;
int len = nums.length;
int max = nums[0];
int min = nums[len-1];
for(int i = 0; i<len; i++){
max = Math.max(max, nums[i]);
if(max != nums[i]){
e = i;
}
min = Math.min(min, nums[len-1-i]);
if(min != nums[len-1-i]){
s = len-1-i;
}
}
return e-s+1;
}
}
LeetCode Shortest Unsorted Continuous Subarray的更多相关文章
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this 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: 使用一个辅助数组,新建一个跟原数组一模一 ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)
问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- Shortest Unsorted Continuous Subarray LT581
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 ...
随机推荐
- javaweb学习纲要
Java Web 概述 大纲: 1.C/S体系结构 2.B/S体系机构 3.两种体系结构比较 4.主流的Web程序应用平台 5.java web学习路线图 1.C/S体系结构 C/S是Client/ ...
- 前端之 Ajax(补)
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- Kattis - register 【水】
题意 就是 有一堆容器,然后可以执行加的操作,每个容量是 2, 3, 5, 7, 11, 13, 17, 19 然后 有进位 比如第一个 容器,到2了,就会重置为0,然后 下一个容器+ 1, 但是要保 ...
- C++中引用编译过的C代码为什么要用“extern c”
函数经过编译系统的翻译成汇编,函数名对应着汇编标号. 因为C编译函数名与得到的汇编代号基本一样,如:fun()=>_fun, main=>_main 但是C++中函数名与得到的汇编代号 ...
- ETL应用:一种处理接口的Pro*C实现方法
2007年,当时项目所有ETL采用C编写,实现了ETL基本功能.当接口很多时,为保证文件获取效率,做好接口可配置:文件维护中经常会出现接口晚到情况,需要有一种方法能将接口晚到信息写入数据库,便于短信告 ...
- 在看 jquery 源码中发现的一些优化方向
1. 避免使用 $.fn.each 或 $.each 因为它比原生的 for/while 真的会慢一些,循环次数越多差距越大. 另外,对象的 for-in 比 for 是要快一丢丢的,但数组的 for ...
- VoLTE的前世今生...说清楚VoIP、VoLTE、CSFB、VoWiFi、SIP、IMS那些事...
转:https://mp.weixin.qq.com/s?__biz=MzA3MTA3OTIwMw==&mid=401344844&idx=1&sn=497b351f524af ...
- Python3.4 用 pip 安装lxml时出现 “Unable to find vcvarsall.bat ”
我的python版本是Python 3.5 该问题的产生是在windows环境中,python 的 Setup需要调用一个vcvarsall.bat的文件,该文件需要安装c++编程环境才会有.网上的方 ...
- PowerDesigner生成数据库表和逆向生成表结构(MySQL数据库)
一.Download Connector/ODBC下载ODBC驱动,地址:https://dev.mysql.com/downloads/connector/odbc/, 需要注意:PowerDesi ...
- hdu 4737 A Bit Fun 尺取法
A Bit Fun Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...