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 ...
随机推荐
- Java集合总结之Collection整体框架
前段时间一直在忙一个物联网的项目,所以Java的学习一直搁置,从今天开始继续学习!望大家多提宝贵意见! java.util包中包含了一些在Java 2中新增加的最令人兴奋的增强功能:类集.一个类集(c ...
- AtCoder Grand Contest 017 迟到记
晚上去操场上浪. 回来以后看到好几个人开着 \(AtCoder\) 在打代码. ... ... 今天有 \(AtCoder\) 比赛 ? 管它呢, \(Kito\) 在切西瓜,先吃西瓜... 然后看 ...
- 【POJ 3179】 Corral the Cows
[题目链接] http://poj.org/problem?id=3179 [算法] 首先,我们发现答案是具有单调性的,也就是说,如果边长为C的正方形可以,那么比边长C大的正方形也可以,因此,可以二分 ...
- UOJ #188 Sanrd —— min_25筛
题目:http://uoj.ac/problem/188 参考博客:https://www.cnblogs.com/cjoieryl/p/10149748.html 关键是枚举最小质因子...所以构造 ...
- 为IIS Host ASP.NET Web Api添加Owin Middleware
将OWIN App部署在IIS上 要想将Owin App部署在IIS上,只添加Package:Microsoft.OWIN.Host.SystemWeb包即可.它提供了所有Owin配置,Middlew ...
- php 权限 管理
权限的思考: https://www.jianshu.com/p/cf9077a7d38a 权限例子,用户 角色 功能 用户角色关联表 角色功能关联表 http://www.cnblogs.com/n ...
- 微信 unionid 获取 解密数据
1.申请注册微信开放平台 open.weixin.qq.com 2.绑定公众号或者小程序到微信开放平台 3.微信公众号的话,使用微信网页授权获取 unionid https://mp.weixin. ...
- Redis数据清除问题
Redis中数据清除可以分为两种方式 手动清除:指定要清除的key,通过delete命令即可清除 自动清除:使用Redis提供的数据过期策略 Redis数据过期策略 redis提供了非常灵活 ...
- java代码求分数等级的输出~~~
总结:无论是switch-case-break语句 都不要忘了跳出当前循环,即break; 还有这个输入的分数我如何控制在100以内???? package com.c2; //实现分数等级的输出. ...
- yield-from示例
#!/usr/bin/python3# -*- coding: utf-8 -*-# @Time : 2018/6/20 9:13# @File : yield_from11.py fro ...