LeetCode 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.
Note:
- Then length of the input array is in range [1, 10,000].
- The input array may contain duplicates, so ascending order here means <=.
题目标签:Array
题目给了我们一个nums array, 让我们找出一个最短的无序连续子数组,当我们把这个子数组排序之后,整个array就已经是排序的了。
要找到这个子数组的范围,先要了解这个范围的beg 和 end 是如何定义的。
来看这个例子:1 5 4 5
a. 当我们找到第一个违反ascending 排序的数字 2的时候,我们不能是仅仅把beg 标记为2的前面一个数字7,而是要一直往前,找到一个合适的位置,找到在最前面位置的比2大的数字,这里是3。
b. 同样的,为了找end, 那么我们要从7的后面开始找,一直找到一个最后面位置的比7小的数字,这里是6。
这样的话,范围就是3到6 是我们要找的子数组。把3到6排序完了之后,整个array 就已经是排序的了。
这里我们可以发现,2是min, 7是max,所以我们可以分两个方向来分别寻找beg 和end。
从右到左(绿色),维护更新min 和 beg;
从左到右(红色),维护更新max 和 end。
Java Solution:
Runtime beats 89.16%
完成日期:10/15/2017
关键词:Array
关键点:分别以两个方向来找到beg 和 end
class Solution
{
public int findUnsortedSubarray(int[] nums)
{
int n = nums.length;
int beg = -1;
int end = -2; // end is -2 is because it works if the array is already in ascending order
int min = nums[n-1]; // from right to left
int max = nums[0]; // from left to right for(int i=0; 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)
beg = n-1-i;
} return end - beg + 1; // if array is already in ascending order, -2 - (-1) + 1 = 0
}
}
参考资料:
https://discuss.leetcode.com/topic/89282/java-o-n-time-o-1-space
LeetCode 题目列表 - LeetCode Questions List
LeetCode 581. 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 ...
- Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8, 1 ...
- 【leetcode_easy】581. Shortest Unsorted Continuous Subarray
problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...
- 581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...
- Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)
581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...
- [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】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- 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_Easy tag: Sort, Stack
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
随机推荐
- Day-17: 网络编程
---恢复内容开始--- 现有的互联网通讯方式,是服务器端的进程与客户端进程的通信.Python中进行网络编程,就是在Python程序本身这个进程内,连接别的服务器进程的通信端口进行通信. 互联网协议 ...
- Servlet第五篇【介绍会话技术、Cookie的API、详解、应用】
什么是会话技术 基本概念: 指用户开一个浏览器,访问一个网站,只要不关闭该浏览器,不管该用户点击多少个超链接,访问多少资源,直到用户关闭浏览器,整个这个过程我们称为一次会话. 为什么我们要使用会话技术 ...
- Hibernate第八篇【懒加载】
前言 前面在使用Hibernate的时候就提及过了懒加载,但没有好好地说明具体的说明究竟是怎么回事-本博文主要讲解懒加载 什么是拦截器以及为什么要使用懒加载? 懒加载就是当使用数据的时候才去获取数据. ...
- JVM锁机制之synchronized
概述: synchronized是java用于处理多线程同步的一个关键字,用于标记一个方法/代码块,使之成为同步方法/同步块. 用synchronized可以避免多线程处理时的竞态条件问题. 相关概念 ...
- Apache Spark 2.2.0 中文文档 - Spark 编程指南 | ApacheCN
Spark 编程指南 概述 Spark 依赖 初始化 Spark 使用 Shell 弹性分布式数据集 (RDDs) 并行集合 外部 Datasets(数据集) RDD 操作 基础 传递 Functio ...
- 框架应用:Mybatis (一) - 入门案例
ORM框架 在实际开发中,工程中本质的任务是从数据库中获取数据,然后对数据进行操作,又或者写入数据.开发时语言是大多是面向对象的工程语言,这个时候就必须进行工程语言和数据库连接语言的转换,也就是所谓的 ...
- SpringBoot开发案例之mail中文附件乱码
前一段时间做过一个邮件发送的服务,以前大体都测试过,文本.图片.附件都是没有问题的,可有同事反应发送的附件名称有中文乱码,类似如下截图展示: 咋一看不像乱码,抱着试试看的态度,为MimeMessage ...
- linux/Windows系统如何安装PHP-openssl扩展
今天倒腾了半天公司的OA办公系统,原来现在很多的smtp服务器是需要ssl方式加密的,而支持ssl需要php加载openssl扩展.所以本文我们将和大家一起分享如何在linux/Windows系统下安 ...
- Android性能优化xml之<include>、<merge>、<ViewStub>标签的使用
一.使用<include>标签对"重复代码"进行复用 <include>标签是我们进行Android开发中经常用到的标签,比如多个界面都同样用到了一个左侧筛 ...
- 云计算-openstack基础构架以及服务方式详解
一:什么是openstack 是Rackspace(美国航天局)和NASA(一家公司)共同发起的开源项目,他是一系列软件项目的组合. 这些项目是松耦合的关系,可以进行独立的安装,启动和停止,只有在必要 ...