[leetcode-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.
Note:
Then length of the input array is in range [1, 10,000].
The input array may contain duplicates, so ascending order here means <=.
思路:
用一个map记录初始每一个位置对应的数字,排序后,再从两头依次判断是否是原来的数字。
int findUnsortedSubarray(vector<int>& nums)
{
map<int, int>record;
for (int i = ; i < nums.size();i++) record[i] = nums[i]; sort(nums.begin(), nums.end());
int start = , end = nums.size()-;
while (start< nums.size() && nums[start] == record[start]) start++;
while (end >start && nums[end] == record[end]) end--;
return end - start + ;
}
[leetcode-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_easy】581. Shortest Unsorted Continuous Subarray
problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- [LeetCode] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack
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 ...
- 【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 ...
- LeetCode Shortest Unsorted Continuous Subarray
原题链接在这里:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/ 题目: Given a ...
随机推荐
- IO多路复用深入浅出
前言 从零单排高性能问题,这次轮到异步通信了.这个领域入门有点难,需要了解UNIX五种IO模型和 TCP协议,熟练使用三大异步通信框架:Netty.NodeJS.Tornado.目前所有标榜异步的通信 ...
- wifi驱动总结(1)
一.wifi平台设备驱动注册过程Path:Rtw_android.c (rk3399\kernel\drivers\net\wireless\rockchip_wlan\rtl8723au\os_de ...
- FutureTask——另一种闭锁的实现
关于多线程,我们接触对多的,最基础,入门的可能就是实现Runnable接口继承Thead类,因为Java单继承的原因,通常建议是实现Runnable接口.但这种"简单"的线程会带来 ...
- python csv例子
import csv fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] rows = [{'Column1': '0', 'Colum ...
- rPithon vs. rPython(转)
Similar to rPython, the rPithon package (http://rpithon.r-forge.r-project.org) allows users to execu ...
- python爬虫从入门到放弃(四)之 Requests库的基本使用
什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...
- hadoop 2.7.3 集群安装
三台虚拟机,centos6.5 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 :: loca ...
- python学习第三个坑
##########################python 第三章 ################################这一章呢,主要是文件的操作,还有涉及到函数的一部分. PS:整 ...
- [HDU1001] Sum Problem
Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, ...
- sql备份(.bak文件备份)
第一步: 右键需要备份的数据库(这里以 MyDB 为例)-->任务-->备份