public class Solution {
public int FindUnsortedSubarray(int[] nums) {
int n = nums.Length, beg = -, end = -, min = nums[n - ], max = nums[];
for (int i = ; i < n; i++)
{
max = Math.Max(max, nums[i]);
min = Math.Min(min, nums[n - - i]);
if (nums[i] < max)
{
end = i;
}
if (nums[n - - i] > min)
{
beg = n - - i;
}
}
return end - beg + ;
}
}

https://leetcode.com/problems/shortest-unsorted-continuous-subarray/#/description

另外一种比较容易理解的方案:

public class Solution {
public int FindUnsortedSubarray(int[] nums) {
int n = nums.Length;
var temp = new List<int>();
for (int i = ; i < n; i++)
{
temp.Add(nums[i]);
}
temp = temp.OrderBy(x => x).ToList(); int start = ;
while (start < n && nums[start] == temp[start])
{
start++;
}
//正序找到第一个不同,确定start的位置 int end = n - ;
while (end > start && nums[end] == temp[end])
{
end--;
}
//逆序找到第一个不同,确定end的位置 return end - start + ;
}
}

leetcode581的更多相关文章

  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. LeetCode581. Shortest Unsorted Continuous Subarray

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

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

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

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

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

  5. Leetcode刷题之矩阵中的指针用法

    矩阵中的指针用法 1 快慢指针 ​ Leetcode27移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度.不要使用额外的数组 ...

随机推荐

  1. PLSQL Developer连接Oracle

    1.安装PLSQL Developer Oracle数据库和PL/SQL Developer的安装步骤,这里就不做叙述了,百度安装方法的时候有说在安装PL/SQL Developer软件时,不要安装在 ...

  2. Oracle物化视图梳理

    --物化视图可以分为三种类型:* 包含聚集的物化视图* 只包含连接的物化视图* 嵌套物化视图三种物化视图的快速刷新的限制条件有很大区别,而对于其他方面则区别不大. --物化视图创建方式(Build M ...

  3. Codeforces Round #309 (Div. 1) A(组合数学)

    题目:http://codeforces.com/contest/553/problem/A 题意:给你k个颜色的球,下面k行代表每个颜色的球有多少个,规定第i种颜色的球的最后一个在第i-1种颜色的球 ...

  4. springcloud学习总结

    最近花了一周的时间对springcloud的常用组件进行了一些入门级的了解,也破天荒的积极起来用博客进行了学习的记录,只是希望以后用的时候能对自己有所帮助,也希望给跟我一样对springcloud毫无 ...

  5. HTML知识点梳理1

    1,HTML基本结构 <!DOCTYPE html> <html> <head></head> <body> </body> & ...

  6. 帝国cms自动保存图片

    <?=ECMS_ShowEditorVar("newstext",$ecmsfirstpost==1?"":stripSlashes($r[newstex ...

  7. freebsd安装python2

    第一步  cd /usr/ports/lang/python27 第二步 输入以下命令(需要联网) make make install 到此如果输入python无效   继续 第三步 cd /usr/ ...

  8. 安装win10 1703版本操作系统

    1.使用UltraISO 全功能单文件 9.5.3.2900刻录工具,刻录iso文件到U盘里 2.默认刻录之后,U盘分区格式变成了fat32,而fat32单个文件无法超过4GB 而1703版本里面的i ...

  9. Java线程及线程池状态

    一.Java线程的六种状态 如上图1,JDK定义线程状态是不存在“运行中”状态,但为方便描述过程有些图中会画出运行中的状态. Java线程创建后调用start方法进入就绪状态,被OS调度选中后运行,运 ...

  10. QQ群成员发言次数统计(正则表达式版)

    1.先将QQ群的消息记录以.txt文件格式导出来,保存路径及名称自己定义(在本文我导出到Y盘,命名为test.txt) 2.程序如下: data statistics1; if _n_=1 then ...