Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
你找到的子数组应是最短的,请输出它的长度。
示例 1:
输入: [2, 6, 4, 8, 10, 9, 15] 输出: 5 解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
说明 :
- 输入的数组长度范围在 [1, 10,000]。
- 输入的数组可能包含重复元素 ,所以升序的意思是<=。
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums)
{
int len = nums.size();
if(len <= 1)
return 0;
vector<int> temp = nums;
sort(temp.begin(), temp.end());
int p1 = 0, p2 = len - 1;
for(int i = 0; i < len; i++)
{
if(nums[i] != temp[p1])
break;
p1++;
}
for(int i = len - 1; i >= 0; i--)
{
if(nums[i] != temp[p2])
break;
p2--;
}
return p2 - p1 <= 0? 0 : p2 - p1 + 1;
/*
vector<int> res;
for(int i = p1; i <= p2; i++)
{
res.push_back(nums[i]);
}
return res;
*/
}
};
Leetcode581.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 ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous 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 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
- LeetCode581. Shortest Unsorted Continuous Subarray
Description Given an integer array, you need to find one continuous subarray that if you only sort t ...
- LeetCode 最短无序连续子数组
题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L ...
- leetcode最短无序连续子数组
平民解法: 既然是找最小数组,那就得到一个排序好的数组,然后直接和初试数组比对,用一个left,right分别记录从最初开始不同,到最后不同的小标,最后左右做差再加一,就能得到长度. 其他解法: 双指 ...
- 【leetcode_easy】581. Shortest Unsorted Continuous Subarray
problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...
- 581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...
随机推荐
- 原生ajax封装及用法
/* 封装ajax函数 * @param {string}opt.type http连接的方式,包括POST和GET两种方式 * @param {string}opt.url 发送请求的url * @ ...
- Censoring【自动AC机】【水题毁我青春】【20190614】
这题简直比注水猪肉还水QAQ. 以前做过KMP的Censoring单串匹配,果断选择自动AC机w 对短串建自动AC机 长串去机子里匹配 用个栈边匹配边弹出 记得弹出一个串后把匹配点指向栈顶就ojbk ...
- PAT甲级——A1089 Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 设置listContrl中指定行的颜色
在MFC中 自己通过手动拖放CListCtrl控件来制作自己的表格: 目的: 将指定item的行更该颜色: 步骤: 1,在窗口中拖放CListCtrl控件, 单击右键 创建控件对象: CListCtr ...
- PostgreSQL DISTINCT ON
https://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group select DISTINCT ...
- List -- 循环操作
1,单元循环 for…in 2,索引循环 for…in range(len(List)) 3,同时循环单元和索引 使用enumerate: for index, item in enumerate(L ...
- HZOI2019 星际旅行 欧拉路
题目大意:https://www.cnblogs.com/Juve/articles/11207540.html—————————> 题解:网上都是一句话题解:将所有的边拆成两条,问题变成去掉两 ...
- Java内功修炼系列一反射
“JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制 ...
- PAT甲级——A1033 To Fill or Not to Fill
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...
- 再谈MFC学习——模态对话框的数据传递
学习c++的目的就是为了使用封装好的类.而最最经典的封装类当然是微软的MFC窗体程序类. 学习MFC编程之前要学习c++的主要知识,掌握c++的基本编程思想. 以下就看下我学习的MFC模态对话框的数据 ...