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 ...
随机推荐
- linux(redhat) mysql 的安装
教程链接 注意 1.检查版本32/64位的时候 输入 name -a 输出为 Linux localhost.localdomain 2.6.18-53.el5 #1 SMP Wed Oct 10 1 ...
- fedora 安装新字体 courier new xxx
fedora安装新字体 1.将windows字体拷贝到/usr/share/fonts/truetype下面,文件夹名字可以随便起 cp /media/c/WINDOWS/Fonts/* /usr/s ...
- BZOJ - 2460 :元素 (贪心&线性基)
相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石.一般地,矿石越多则法力越强,但物极必反:有时,人们 ...
- SqlServer2008 新建服务器对象->链接服务器脚本
exec sp_addlinkedserver 'ddxx', '', 'SQLOLEDB','1.192.168.220'exec sp_addlinkedsrvlogin 'ddxx','fa ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
传送门 题目大意:形成一个环的牛可以跳舞,几个环连在一起是个小组,求几个小组. 题解:tarjian缩点后,求缩的点包含的原来的点数大于1的个数. 代码: #include<iostream&g ...
- webpack新版本4.12应用九(配置文件之输出(output))
output 位于对象最顶级键(key),包括了一组选项,指示 webpack 如何去输出.以及在哪里输出你的「bundle.asset 和其他你所打包或使用 webpack 载入的任何内容」. ou ...
- 七、Jmeter + ant + jenkins轻量级接口自动化测试
七.Jmeter + ant + jenkins轻量级接口自动化测试 杀猪不用牛刀,工具没有牛逼高大尚之分,每个工具都有存在的理由:关键是看会不会用,怎么用,有没有用在合适的地方. 需要安装的工具: ...
- switch控件的使用
java中: public class MainActivity extends Activity implements OnCheckedChangeListener{ private Switch ...
- php调试时echo,print_r(),var_dump()的区别
简单说: var_dump() 能打印出类型 print_r() 只能打出值echo() 是正常输出... 需要精确调试的时候用 var_dump();一般查看的时候用 print_r() 另外 , ...
- mongodb数据操作(CRUD)
1.数据插入db.集合名.insert() 操作 > use hk switched to db hk > show collections > db.info.insert({&q ...