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. Ansible 任务计时

    在 github 发现一个 Ansible 任务计时插件“ansible-profile”,安装这个插件后会显示 ansible-playbook 执行每一个任务所花费的时间.Github 地址: h ...

  2. Front-end: Using blurred backgrounds with contents unaffected.

    Purpose: Using a picture as the background of a page with blurred effect, while the content not blur ...

  3. 冒泡排序到k趟

    浙大pat题目 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们.通过一遍扫描,则最后一个元素必定是最大的元素.然后用同样的方 ...

  4. 读书笔记--《gongchandang宣言》

    纪念马克思诞辰200周年 末尾 gongchandang人不屑于隐瞒自己的观点和意图.他们公开宣布:他们的目的只有用暴力推翻全部现存的社会制度才能达到. 让统治接机在共产主义革命面前发抖吧. 无产者在 ...

  5. 《JavaScript 设计模式与开发实战》第一部分(1、2、3章)笔记

    第1章:面向对象的JavaScript 动态类型和鸭子类型 编程语言按照数据类型大体可以分为两类: ① 静态类型语言:在编译时便已确定变量的类型. ② 动态类型语言:变量类型要到程序运行的时候,待变量 ...

  6. AWS Redshift typical error and potential root cause:

    Full join issue: When use full join, the below join condition should not occur: 1, OR statement2, an ...

  7. Codeforces1062A. A Prank(暴力)

    题目链接:传送门 题目: A. A Prank time limit per test second memory limit per test megabytes input standard in ...

  8. 软件工程 week 03

    一.效能分析 1.作业地址:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2139 2.git地址:https://git.coding.ne ...

  9. alpha冲刺(4/10)

    前言 队名:旅法师 作业链接 队长博客 燃尽图 会议 会议照片 会议内容 陈晓彬(组长) 今日进展: 召开会议 安排任务 博客撰写 问题困扰: pm和程序猿的交流到底应该怎么样呢,会出现,不做安排的任 ...

  10. IO流小笔记

    File file=new File ();括号里面写路径 exists()判断文件是否存在:isfile()是判断已经存在的文件是文件还是目录: mkdir()和createNewFile()区别在 ...