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数据库操作(使用FMDB)

    iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...

  2. Jackson使用ObjectManage#readValue传入泛型T的问题

    说明:没找到合适的方法,持续关注这个问题 参考: https://stackoverflow.com/questions/11664894/jackson-deserialize-using-gene ...

  3. Linux中线程的挂起与恢复(进程暂停)

    http://www.linuxidc.com/Linux/2013-09/90156.htm 今天在网上查了一下Linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制.也没有完整 ...

  4. Inno Setup入门(十三)——Pascal脚本(2)

    事件函数(2) function CheckPassword(Password: String): Boolean; 如果安装程序在Pascal 脚本中发现该函数,它自动显示密码页并调用CheckPa ...

  5. isNaN使用的注意事项

    NaN是JavaScript的特殊值,表示 Not a Number 用法: isNaN(numValue); 如果值是 NaN, 那么 isNaN 函数返回 true ,否则返回 false . 注 ...

  6. mac 切换默认python版本

    https://www.zhihu.com/question/30941329 首先终端的“python”命令会执行/usr/local/bin下的“python”链接,链接相当于win下的快捷方式, ...

  7. linux基础-第二十单元_计划任务crond服务

    第二十单元 计划任务crond服务 什么是计划任务:后台运行,到了预定的时间就会自动执行的任务,前提是:事先手动将计划任务设定好.这就用到了crond服务 crond服务相关的软件包[root@MiW ...

  8. oracle数据库修改编码

    (1)SYSTEM  用户登录SQLPLUS        SYS是sysdba用户,不能直接登录 (2)SYSDBA登录        CONN  / as sysdba; (3)查看数据库字符集 ...

  9. grep怎样匹配tab键

    grep怎样匹配tab键 学习了:https://blog.csdn.net/qixinkui/article/details/2746433 1 grep -P '/t'; 2 awk '//t/' ...

  10. 转:ios的crash框架方法论

    http://www.cocoachina.com/ios/20150701/12301.html 1. 其中提到的提高ios崩溃率的用法.