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. 思路:
第一遍从左往右扫,看当前值是否比找到的最大值还小,如果是这样的话,明显中间不是递增的。同样,从右往左扫,如果当前值比已经找到的最小值大,那么中间部分也不是递增的。
class Solution {
public int findUnsortedSubarray(int[] nums) {
if (nums == null || nums.length == || nums.length == ) return ;
int max = Integer.MIN_VALUE, end = -;
// iterate from beginning of array find the last element which is smaller than
// the last seen max from its left side and mark it as end
for (int i = ; i < nums.length; i++) {
max = Math.max(max, nums[i]);
if (nums[i] < max)
end = i;
}
if (end == -) return ;
int min = Integer.MAX_VALUE, begin = -;
for (int i = nums.length - ; i >= ; i--) {
min = Math.min(min, nums[i]);
if (nums[i] > min)
begin = i;
}
return end - begin + ;
}
}
Shortest Unsorted Continuous Subarray的更多相关文章
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- 【leetcode_easy】581. Shortest Unsorted Continuous Subarray
problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...
- Shortest Unsorted Continuous Subarray LT581
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- Leeetcode--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 ...
- 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况
[抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...
- LeetCode581. Shortest Unsorted Continuous Subarray
Description Given an integer array, you need to find one continuous subarray that if you only sort t ...
随机推荐
- focusout([data],fn) 当元素失去焦点时触发 focusout 事件。
focusout([data],fn) 概述 当元素失去焦点时触发 focusout 事件. focusout事件跟blur事件区别在于,他可以在父元素上检测子元素失去焦点的情况.大理石平台怎么样 参 ...
- java获取一段字符串里符合日期的正则表达式的字符串
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test3 { public static v ...
- BigDecimal 3个toString()方法区别
BigDecimal 的toEngineeringString.toPlainString和toString方法的区别: toEngineeringString:有必要时使用工程计数法.工程记数法是一 ...
- 安装更新npm和nodejs
1.安装npm sudo apt-get install npm 2.升级npm sudo npm install npm@latest -g 3.安装用于安装nodejs的模块n sudo npm ...
- RNN(一)——RNN和LSTM原理
背景 神经网络,卷积神经网络等其他深度学习算法,都有个局限性,各个输入在算法内部是相对独立的.比如:'星际争霸有意思,我爱玩'这句话,是有上下文关系的. 如果放在其他网络里面,各个分词将会独立处理.但 ...
- 获取 Django版本号的两种方式
one k@ubuntu:~$ python Python ( , ::) [GCC ] on linux2 Type "help", "copyright", ...
- instr动态模糊查询
String sqlSearchtext = ""; if(!"".equals(model.getXzqhdm())&&model.getXz ...
- SSH交互式脚本StrictHostKeyChecking选项 benchmode=yes
SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击.但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. 什 ...
- 记录学习Linux遇到的问题
shl@shl-tx:~$ ifconfig Command 'ifconfig' not found, but can be installed with: sudo apt install net ...
- 18.flannel
18.flannel docker有四种常用网络 bridge joined opended 直接共享使用节点的网络名称空间 none k8s网络通信: 容器间的通信: 同一个Pod内的多个容器之间通 ...