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 ...
随机推荐
- SpringMVC使用session实现简单登录
1.首先为了能直观地在jsp页面体现session的内容,我使用了jstl表达式,首先在pom.xml中引入jstl的依赖 <!-- jstl所需要的依赖 --> <dependen ...
- self = [super init] 最终解释
答: init 中调用super的 init方法来初始化自己所包含有的父类信息 1.内存分配 内存应该在[Class alloc]的时候就已经分配了,大小和类型应该由对应的Clas ...
- 安卓开发第一记 android stdio 安装后 新建测试项目报错
Failed to resolve:com.android.support:appcompat-v7:报错处理 你在使用android studio时是否也出现过上图的报错,你还在为它的出现烦恼? ...
- SqlServer 数据库/数据表 拆分(分布式)【转】
通过某种特定的条件,将存放在同一个数据库中的数据分散存放到多个数据库上,实现分布存储,通过路由规则路由访问特定的数据库,这样一来每次访问面对的就不是单台服务器了,而是N台服务器,这样就可以降低单台机器 ...
- python的正则re模块
一. python的正则 python的正则模块re,是其内置模块,可以直接导入,即import re.python的正则和其他应用的正则及其相似,有其他基础的话,学起来还是比较简单的. 二. 正则前 ...
- 自定义springmvc统一异常处理器(实现HandlerExceptionResolver接口)不起作用的一种情况
ExceptionResolverCustom 这个是自定义的异常处理器类. 在springmvc中注册 在web.xml文件中屏蔽springmvc自动注册的异常处理器 网上的资料就是这么配置的,可 ...
- django的manytomany总结
from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagli ...
- ArcGIS_Server的安装
1.双击ArcGIS_for_Server_Windows_103_142101.exe 2.下一步 3.关闭 4.Win10系统弹出询问框是否更改程序,点击“”是“” 5.开始安装程序,点击next ...
- Python 函数 min()
min() 函数 作用: min() 方法返回给定参数的最小值,参数可以为序列.x-数值表达式.y-数值表达式.z-数值表达式.返回给定参数的最小值. 语法: min( x, y, z, .... ...
- 记录一下 C51 里的位运算
记录一下 C51 里的位运算 一篇上个世纪的文章<单片机的C语言中位操作用法>1 今天看到一个这样的运算,这相当于清了 XICFG. #define INT1IS1 0x80 #defin ...