Description

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 <=.

my program

思路:构建一个排序好的数组,然后与原数组进行对比,找出最先和最后不同的元素,相减+1即为所求答案。此算法时间复杂度是O(nlogn),空间复杂度是O(n).

class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
vector<int> tmp = nums;
sort(tmp.begin(), tmp.end());
int i = 0;
int j = nums.size() -1;
for (; i< nums.size(); i++) {
if (nums[i] != tmp[i])
break;
}
if (i >= nums.size())
return 0;
for (; j > 0; j--) {
if (nums[j] != tmp[j])
break;
}
return j - i + 1;
}
};

Submission Details

307 / 307 test cases passed.

Status: Accepted

Runtime: 56 ms

解法二

/**
* /------------\
* nums: [2, 6, 4, 8, 10, 9, 15]
* minr: 2 4 4 8 9 9 15
* <--------------------
* maxl: 2 6 6 8 10 10 15
* -------------------->
*/
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int n = nums.size();
vector<int> maxlhs(n); // max number from left to cur
vector<int> minrhs(n); // min number from right to cur
for (int i = n - 1, minr = INT_MAX; i >= 0; i--) minrhs[i] = minr = min(minr, nums[i]);
for (int i = 0, maxl = INT_MIN; i < n; i++) maxlhs[i] = maxl = max(maxl, nums[i]); int i = 0, j = n - 1;
while (i < n && nums[i] <= minrhs[i]) i++;
while (j > i && nums[j] >= maxlhs[j]) j--; return j + 1 - i;
}
};

此算法时间复杂度仅是O(n),空间复杂度是O(n). 优于第一种算法

LeetCode581. Shortest Unsorted Continuous Subarray的更多相关文章

  1. Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组

    给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8, 1 ...

  2. LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)

    581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...

  3. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

    problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...

  4. [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  5. Shortest Unsorted Continuous Subarray LT581

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  6. LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  7. [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  8. Leeetcode--581. Shortest Unsorted Continuous Subarray

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  9. 581. Shortest Unsorted Continuous Subarray

      Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...

随机推荐

  1. Android闪闪发光字体效果

    原文: http://blog.csdn.net/xu_fu/article/details/24484019 import android.content.Context; import andro ...

  2. Qemu 有用的链接

    Qemu下载和编译 Download https://en.wikibooks.org/wiki/QEMU/Linux https://en.wikibooks.org/wiki/QEMU/Insta ...

  3. OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决

    这问题根本不是DirectX问题,不知道网上怎么这么这样的回复.而且也不亲自验证一下.下面将自己花很多时间才解决的方式整理一下. 因为一般情况下你配置的OpenCV加入系统环境变量的都是X86下的bi ...

  4. WebForm页面使用Ajax

    AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML.指一种创建交互式网页应用的网页开发技术.AJAX并非缩写词,而是由Jesse ...

  5. Queue 队列的用法

    队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用. 以下实例演示了队 ...

  6. 【Sofa】Sofa比赛成绩记录

    最高得到过第4名,然后后面跌倒了第7名,现在追到了第6名.虽然名次还不是最高,但是很开心,今天能在一道一直困扰的题目上有突破,就是那个自行车预测的题目,开始过拟合了.后面进行了一些处理,效果很明显.继 ...

  7. javascript快速入门12--函数式与面向对象

    函数 函数是一组可以随时随地运行的语句. 函数是 ECMAScript 的核心. 创建函数 function fnOne() {//具有名称的函数,函数名必须符合变量名命名规范 //可以没有符何语句 ...

  8. shell--管道命令(pipe)

    管道命令使用的是“|”这个界定符号 管道命令“|”仅能处理经由前面一个命令传来的正确信息,也就是standard output的信息,对于standard error并没有直接处理的能力 每个管道后面 ...

  9. Flexbox兼容性语法汇总

    Flexbox版本 flexbox从第一次出现至今总共有三个语法版本,他们分别是: "display:box;"  —  2009年的老版本 "display:flexb ...

  10. Java8 对多个异步任务进行流水线操作(笔记)

    现在我们要对商店商品进行折扣服务.每个折扣代码对应不同的折扣率,使用一个枚举变量Discount.Code来实现这一想法,具体代码如下所示. 以枚举类型定义的折扣代码 /** * 折扣服务api * ...