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 的元素,并返回移除后数组的新长度.不要使用额外的数组 ...
随机推荐
- Alpha冲刺5
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9989898.html 作业博客:https://edu.cnblogs.com/campus/ ...
- iOS 证书申请和使用详解(详细版)阅读
对于iOS开发者来说,apple开发者账号肯定不会陌生.在开发中我们离不开它.下面我简单的为大家分享一下关于iOS开发中所用的证书相关知识. 第一部分:成员介绍 1.Certification(证书) ...
- 洛谷P1219 :八皇后(DFS+回溯)
题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...
- #考研笔记#计算机之word问题
Word 问题:1. 如何为文档加密保存?单击 office 按钮\另存为\工具按钮\常规选项\设置打开文件时的密码 2. 怎样在横格稿纸中录入古诗?单击 office 按钮\新建\模板\信纸\稿纸( ...
- sails.js mvc framework learning
目的:加快开发速度,总结使用方法: menu list: custom controller custom 模块使用 custom model custom middleware custom ser ...
- 将文本转化为Numpy的矩阵
def file2matrix(filename): fr = open(filename) numberOfLines = len(fr.readlines()) #get the number o ...
- easyUI默认图标的使用
使用格式如下: <table id="table" class="easyui-datagrid" style="width:600px;hei ...
- Android的发展历史
Android一词最早出现于法国作家利尔亚当(Auguste Villiers de l’Isle-Adam)在1886年发表的科幻小说<未来夏娃>(L’ève future)中.他将外表 ...
- Qt对`vtable的未定义引用
错误描述:Qt在linux系统编译时,遇到一个错误大致如下形式 在 xxxxx函数中 对‘vtable for xxxxx未定义的引用 网上找了很多,各种情况都有,大多数是虚函数未实现导致的, 但也有 ...
- 关于DDR3控制器的使用
关于DDR3控制器的使用 mig_7series_0 u_mig_7series_0 ( // Memory interface ports .ddr3_addr (ddr3_addr), // ou ...