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 的元素,并返回移除后数组的新长度.不要使用额外的数组 ...
随机推荐
- Jmeter的察看结果树 出现乱码的解决方法
在apache-jmeter-3.0\bin目录下, 用Notepad工具打开jmeter.properties文件 在文件中搜索encoding,可以搜素到1030行: #sampleresult. ...
- SharePoint REST API - 使用REST API和jQuery上传一个文件
博客地址:http://blog.csdn.net/FoxDave 本篇主要通过两个代码示例来展示如何应用REST API和jQuery上传文件到SharePoint. 示例会使用REST接口和j ...
- __FILES__
_FILE_ :被称为PHP魔术常量 ,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径 1)dirname(__FILE___) 函数返回的是脚本所在在的路径. 比如文件 b.php ...
- manhattan plots in qqplot2
###manhattan plots in qqplot2library(ggplot2)setwd("~/ncbi/zm/XPCLR/")read.table("LW. ...
- 同一台电脑中同时安装oracle database 服务器端和oracle client 客户端时注意
如果在一台电脑中同时安装oracle的客户端和服务器端软件, 一定要先安装oracle database 服务端,并进行相应的配置 listener.ORA. 然后再去安装oracle client ...
- Yii2事件驱动的运行机制
最近一段时间正在作个一个项目,这个项目会系统逻辑比较复杂,使用PHP Yii2,使用事件驱动机制进行研发,下面就最近研究事件驱动机制的使用作以下总结: 流程如下: 1.要创建含有事件注入的类,一般这样 ...
- 使用Blend设计出符合效果的WPF界面
之前不会用blend,感觉好难的,但美工给出的效果自己有没办法实现,所以研究了一下blend,感觉没有想象中的那么难 废话不多说,开始界面设计 今天拿到美工给的一个界面效果图 这个界面说实话,还可以吧 ...
- java_免费视频课程汇总
xml使用场景 各种配置文件 用于用户界面的开发 传输数据:ajax 这个可能过时,因为程序员更喜欢将xml用json来代替 web service:这些老式的web serv ...
- 十七、springcloud(三)服务的注册与调用
1.启动服务注册中心Eureka(见上篇) 启动成功后,暂时无服务 2.项目框架 3.创建服务提供者(spring-cloud-houge-provider)jar a.application.pro ...
- 汉语言处理工具pyhanlp的拼音转换与字符正则化
汉字转拼音 HanLP中的汉字转拼音功能也十分的强大. 说明: l HanLP不仅支持基础的汉字转拼音,还支持声母.韵母.音调.音标和输入法首字母首声母功能. l HanLP能够识别多音字,也能给繁体 ...