问题

给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。

你找到的子数组应是最短的,请输出它的长度。

输入: [2, 6, 4, 8, 10, 9, 15]

输出: 5

解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。

说明 :

输入的数组长度范围在 [1, 10,000]。

输入的数组可能包含重复元素 ,所以升序的意思是<=。


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.

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


示例

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 2, 6, 4, 8, 10, 9, 15 };
var res = FindUnsortedSubarray(nums);
Console.WriteLine(res); res = FindUnsortedSubarray2(nums);
Console.WriteLine(res); res = FindUnsortedSubarray3(nums);
Console.WriteLine(res); Console.ReadKey();
} private static int FindUnsortedSubarray(int[] nums) {
var newNums = new int[nums.Length];
Array.Copy(nums, newNums, nums.Length);
Array.Sort(newNums);
//先排序并赋初始值
int begin = 0, end = nums.Length - 1;
//如果之前的数据就在正确的位置上,就移动指针位置
while(nums[begin] == newNums[begin] && begin < end)
begin++;
while(nums[end] == newNums[end] && begin < end)
end--;
//相等时,代表之前就是有序的
if(begin == end)
return 0;
else
return end - begin + 1;
} private static int FindUnsortedSubarray2(int[] nums) {
//FindUnsortedSubarray的优化写法
var newNums = new int[nums.Length];
Array.Copy(nums, newNums, nums.Length);
Array.Sort(newNums);
int begin = -1, end = -2;
for(int i = 0; i < nums.Length; i++) {
if(nums[i] != newNums[i]) {
if(begin == -1)
begin = i;
end = i;
}
}
return end - begin + 1;
} private static int FindUnsortedSubarray3(int[] nums) {
//此解法参考英文官方LeetCode上的讨论
//CSDN和cnblog上关于此解法讨论基本上都是错误的
//很多博主参考此解法给出了正确的实现,但是解释的均不对
//至少我本人没有发现任何一篇的解释是对的,经不起推敲,一个反例即可
//其实这个解法和FindUnsortedSubarray解法的基本思路类似
//从左到右(或从右到左)找最小值(或最大值),看这个最小值(或最大值)是否在其应该存在的地方
//例如第1次,找出数组中第1小的值,看其是不是在索引为0的地方
//如果不是,查找结束,start为0
//如果是,找出数组中第2小的值,看其是不是在索引为1的地方
//以此类推,从右向左找最大值的方式类似
//以上分析才是这个解法的精髓所在,思路同FindUnsortedSubarray
//但解法巧妙至极,是一种高度优化的写法
int n = nums.Length;
//赋初始开始和结束值
int start = -1;
int end = -2;
//结束值赋为-2是考虑在数组本身就是有序时
//最后一句的return也可以给出正确值
//因为 end = i 没有被执行
int min = nums[n - 1];
int max = nums[0];
//
for(int i = 0, pos = 0; i < n; i++) {
pos = n - 1 - i;
//找出局部最大、最小值
max = Math.Max(max, nums[i]);
min = Math.Min(min, nums[pos]);
//如果当前值小于局部最大值,重置end
if(nums[i] < max)
end = i;
//如果当前值大于局部最小值,重置start
if(nums[pos] > min)
start = pos;
}
//返回开始和结束的索引差
//假如是1、2,2 - 1 + 1 = 2,因为1,2是2个值
return end - start + 1;
} }

以上给出3种算法实现,以下是这个案例的输出结果:

5
5
5

分析:

显而易见,FindUnsortedSubarray和FindUnsortedSubarray2的时间复杂度基于 Array.Copy() 所使用的排序算法,FindUnsortedSubarray3的时间复杂度为:  。

C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)的更多相关文章

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

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

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

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

  3. Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)

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

  4. [LeetCode] 581. 最短无序连续子数组 ☆

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

  5. Leetcode 581.最短无序连续子数组

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

  6. [LeetCode] 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(Java实现)

    这是悦乐书的第267次更新,第281篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第134题(顺位题号是581).给定一个整数数组,找到一个连续的子数组,按升序对该子数组 ...

  8. 581. Shortest Unsorted Continuous Subarray

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

  9. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

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

随机推荐

  1. 什么?你正在学web自动化测试?那这些Selenium的基本操作你了解过吗?

    在自动化测试中,我们都知道是通过定位元素来实现的,那么有时候我们定位元素定位不到是为什么呢? 1.页面出现了iframe 2.出现了新的窗口,没有实现句柄的切换 3.三种等待方式,没有选择其中之一来使 ...

  2. Python+selenium+unittest+HTMLTestReportCN单元测试框架分享

    分享一个比较基础的,系统性的知识点.Python+selenium+unittest+HTMLTestReportCN单元测试框架分享 Unittest简介 unittest是Python语言的单元测 ...

  3. 导出Telegram贴纸

    如何导出Telegram的贴纸1.在Telegram中 @StickerSetBot 机器人2.输入 /newpack 开启机器人,会提示 OK now send me stickers or sti ...

  4. 题解 洛谷 P5385 【[Cnoi2019]须臾幻境】

    首先我们知道 \(n\) 个点的树有 \(n-1\) 条边,因此对于森林来说,其点数减边数即为树的个数.那么对于普通的图,求出其任意一个生成树森林,森林中树的个数即为原图中连通块的个数,也就是点数减边 ...

  5. 一些非常实用的git命令

    阅读目录 一.前言 二.git branch 和 git checkout 三.git clone 和 git remote 四.git pull 和 git push 五.git merge 和 g ...

  6. linux命令笔记记录(自用)

    1.解除yum锁定: sudo rm -rf /var/run/yum.pid 2.删除文件夹: rm -rf /var/log/httpd/access 3.更新pip: python -m pip ...

  7. Django Models随机获取指定数量数据方法

    方法一:新增models的Manager方法 下面就直接发代码了 class RandomManager(models.Manager): def get_queryset(self): return ...

  8. 基于Scrapy的B站爬虫

    基于Scrapy的B站爬虫 最近又被叫去做爬虫了,不得不拾起两年前搞的东西. 说起来那时也是突发奇想,想到做一个B站的爬虫,然后用的都是最基本的Python的各种库. 不过确实,实现起来还是有点麻烦的 ...

  9. LQB201803乘积尾零

    果然是练思维呀!!要是我的话估计就能挨个算一算呜呜呜 分解成 2和5相乘的式子 #include <iostream> using namespace std; //快速幂运算 int m ...

  10. Python os.openpty() 方法

    概述 os.openpty() 方法用于打开一个新的伪终端对.返回 pty 和 tty的文件描述符.高佣联盟 www.cgewang.com 语法 openpty()方法语法格式如下: os.open ...