leetcode581
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的更多相关文章
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- LeetCode581. Shortest Unsorted Continuous Subarray
Description Given an integer array, you need to find one continuous subarray that if you only sort t ...
- Leetcode581.Shortest Unsorted Continuous Subarray最短无序连续子数组
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8, 1 ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- Leetcode刷题之矩阵中的指针用法
矩阵中的指针用法 1 快慢指针 Leetcode27移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度.不要使用额外的数组 ...
随机推荐
- ios 审核未通过 相机相册权限问题
苹果提交审核被打回来 附加的说明如下: We noticed that your app requests the user’s consent to access their camera but ...
- [多线程] 线程中的synchronized关键字锁
为什么要用锁? 在多线程中,难免会出现在多个线程中对同一个对象的实例变量或者全局静态变量进行并发访问的情况,如果不做正确的同步处理,那么产生的后果就是"脏读",也就是取到的数据其实 ...
- C# 引用类型公共变量的影响
public int[] a =new int[2]; private void button1_Click(object sender, EventArgs e) { bing(a); } priv ...
- TP5 强制下载PDF
为什么叫强制下载 因为你点击你的PDF文件路劲的话 浏览器是默认字网页上打开,而不是下载 我们需要做的就是 修改header头信息 使其变为下载状态 //下载PDF public functi ...
- 基于Verilog的简单FIFO读写实验
一.模块框图及基本思路 fifo_ip:ISE生成的IP fifo_control:在fifo未满情况下不断写入递增的四位数,每隔1s读出一个数据驱动Led显示 fifo_top:前两个模块的组合 二 ...
- linux下vim的安装及其设置细节
第一步:使用apt安装vim sudo apt-get install vim 第二步:行号及其tab建设置 vim ~/.vimrc 添加如下文字 set nu //代码显示行号syntax on ...
- 行高(line-height)
line-height属性 设置元素中文本行高.
- UVa699
这个建树的根选的很有意思,在中间作为树的根.所以二叉树建树的方法虽然一般是有两种数组的方法,一个是如果深度不太大的话,可以之间用2*k+1,2*k建树,如果很大的话,就挨着建树,弄一个结构体,有左右子 ...
- 《DSP using MATLAB》Problem 7.11
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- python------Socket网略编程+动态导入模块
上节课程回顾: 静态变量:与类无关,不能访问类里的任何属性和方法. 类方法:只能访问类变量. 属性:把一个方法变成静态属性, 反射: __new__:先于__init__执行: __call__: c ...