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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
题目有歧义:其实没有“最短”的概念,找到一个范围就行了
[奇葩corner case]:
1234,输出0。因此i小j大的初始值是-1,0。别的地方不知道能否试试?
[思维问题]:
指针对撞一直走,但是没想到最后会ij颠倒大小。
[一句话思路]:
i小j大变成了i大j小,所以结果是i - j + 1
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 不可能i j,l r两对指针同时走的,一对就够了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
指针对撞一直走,但是没想到最后会ij颠倒大小。i - j + 1
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int findUnsortedSubarray(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return 0;
}
//ini: l r
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, i = -1, j = 0;
//for loop
for (int l = 0, r = nums.length - 1; r >= 0; l++, r--) {
max = Math.max(nums[l], max);
if (nums[l] != max) {
i = l;
}
min = Math.min(nums[r], min);
if (nums[r] != min) {
j = r;
}
}
return i - j + 1;
}
}
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 ...
- 【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 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- [LeetCode] 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, ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
随机推荐
- 瀑布流下滑 发送ajax
<!DOCTYPE=html> <html> <head> <meta charset="UTF-8"> ...
- Java [Leetcode 347]Top K Frequent Elements
题目描述: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...
- Docker从入门到安装MySQL
Docker 的简介 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后 ...
- 重温CLR(六)方法和参数
实例构造器和类(引用类型) 构造器是将类型的实例初始化为良好状态的特殊方法.构造器方法在“方法定义元数据表”中始终叫做.ctor(constructor的简称).创建引用类型的实例时,首先为实例的数据 ...
- 【java反射】Class类型的相关操作演练
[一]获取范型接口的实现类的范型类型 (1)范型接口 package org.springframework.context; import java.util.EventListener; publ ...
- 洛谷 P1312 Mayan游戏
题解:搜索+模拟 剪枝: 最优性剪枝:x从小到大,y从小到大,第一次搜到的就是字典序最小 的最优解. 最优性剪枝:把一个格子和左边格子交换,和左边格子和右边格 子交换是等价的,显然让左边格子和右边交换 ...
- JBPM的ORACLE脚本
create table JBPM4_DEPLOYMENT ( DBID_ number(19,0) not null, NAME_ clob, TIMESTAMP_ number(19,0), ST ...
- Oracle查询多边形对象SDO_GEOMETRY并转换为java对象举例
最近实现了一个判断点是否与多边形交互的功能,这里的点是一个经纬度,多边形是一个区域,包含多个经纬度,最后看下这个点是否在这个区域内.就好比你打开百度地图,然后看你自己的位置(点)是不是在某个小区(多边 ...
- java代码====实现按钮点击改变颜色=======
总结: package com.a.b; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event ...
- python selenium中iframe切换、window切换方法
一.selenium中iframe切换方法: 方法一:switch_to.frame frame函数中提供了三种定位方法:by index, name, or webelement. driver.s ...