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. iOS UILabel自定义行间距

    NSString *hintStr = @"输入材料标题搜索材料\n注:可根据材料序号直接搜索, 如TPO23"; CGSize size = [toolset returnTex ...

  2. shell基本计算、逻辑运算、位运算详解

    转:http://blog.chinaunix.net/uid-8504518-id-3918531.html Shell 提供大量的基本运算操作,在脚本中非常有用.Shell 对您提供的算术表达式求 ...

  3. java实现发送邮件功能

    项目中实现发送邮件功能,先书写一个小Demo,记录如下: POM.XML中导入依赖 <!-- start java 提供的支持邮件发送相关业务的类 --> <dependency&g ...

  4. WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree

    ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...

  5. C# SendMail 发送邮件

    最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法 1.利用新浪邮箱发送 2.利用公司邮箱发送 3.利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.no ...

  6. oracle 存储过程的一个小问题

    在存储过程中,一般不能直接使用DDL语句,需要借助 execute immediate方法,否则可能报 pls00103错误 例子如下: create or replace procedure rt_ ...

  7. java压缩 GZIP进行简单压缩,ZIP进行多文件保存

    java压缩  GZIP进行简单压缩,ZIP进行多文件保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6 ...

  8. 使用 Apple Configurator 2 获取ipa文件

    如今很多人想要获取到App Store上的包却苦于无奈,先在要把App Store上的包载下来获取ipa,最直接的就是从手机设备上导出了,但是手机必须要9.0以下才可以导出,鄙人手中正好有公司的测试机 ...

  9. 工作笔记4.struts2上传文件到server

    本文介绍两种:上传文件到server的方式   一种是提交Form表单:还有一种是ajaxfileupload异步上传. 一.JSP中:     1.提交Form表单 为了能完毕文件上传,我们应该将这 ...

  10. Android 4.4 Kitkat Phone工作流程浅析(七)__来电(MT)响铃流程

    本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处 本文代码以MTK平台Android 4.4为分析对象,与Google原生AOSP有些许差异,请读者知悉. ...