problem

581. Shortest Unsorted Continuous Subarray

题意:感觉题意理解的不是非常明白。

solution1:

使用一个辅助数组,新建一个跟原数组一模一样的数组,然后排序。从数组起始位置开始,两个数组相互比较,当对应位置数字不同的时候停止,同理再从末尾开始,对应位置上比较,也是遇到不同的数字时停止,这样中间一段就是最短无序连续子数组。

class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int n = nums.size();
int i = , j = n-;//err.
vector<int> tmp = nums;
sort(tmp.begin(), tmp.end());
while(i<n && tmp[i]==nums[i]) i++;
while(j>i && tmp[j]==nums[j]) j--;//err.
return j-i+; }
};

参考

1. Leetcode_easy_581. Shortest Unsorted Continuous Subarray;

2. Grandyang;

3. C++ O(n) solution;

4. c++ O(n) one-pass solution which beats almost;

【leetcode_easy】581. Shortest Unsorted Continuous Subarray的更多相关文章

  1. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...

  2. 【leetcode】581. Shortest Unsorted Continuous Subarray

    题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /** * @param {number[]} nums * @retur ...

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

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

  4. 581. Shortest Unsorted Continuous Subarray

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

  5. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

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

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

  7. Shortest Unsorted Continuous Subarray LT581

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

  8. [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组

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

  9. [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray

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

随机推荐

  1. 关于七牛云存储配置服务器CNAME的问题

    以前的图片什么的都存放在七牛云(免费的那款)上,七牛相比OSS就是只能创建bucket但不能创建文件夹,这个令人很烦.最近七牛发公告说存储文件的测试域名30天后不能使用了,那我那些存储的图片的所有外链 ...

  2. python 杂记-unittest

    介绍单元测试的好文:https://mp.weixin.qq.com/s/njxc8GXSlc3z_RibK70ROg setUpModule/tearDownModule:在整个模块的开始和结束时被 ...

  3. Luogu 4751 动态DP 模板

    题面 动态求最大独立集 题解 树剖后用矩阵转移. 具体见Tweetuzki的洛谷博客 CODE #include <bits/stdc++.h> using namespace std; ...

  4. [CSS] Change the off-axis Alignment of a Flexed Container with `align-items`

    We changed the axis layout with 'justify-content', and the "off axis" layout is controlled ...

  5. MyBatis 接口注解方式代替mapper.xml

    https://blog.csdn.net/m0_38068812/article/details/86566929 spring boot(8)-mybatis三种动态sql  或者 这个 1. 代 ...

  6. 关于 $.proxy(fn,context,arg) 方法

    <!-- $.proxy(fn,this,agrument); proxy 代理 思考做酒的代理商argument就是代理商 把fn所在的作用域对象的参数/属性 代理给fn执行. fn: 要被调 ...

  7. 数据结构实验之查找一:二叉排序树 (SDUT 3373)

    二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),也称二叉搜索树. #include <stdio.h> #include <s ...

  8. Python操作Jira

    目录 认证 项目(Project) 问题(Issue) 配置域(Fields) 关注者/评论/附件 创建/分配/转换问题 搜索 Jira提供了完善的RESTful API,如果不想直接请求API接口可 ...

  9. C++ 获取二维数组的一维长度

    行长度: ]); 列长度: ][]); ]); int column = lines / row;

  10. Tkinter 之Scale滑块标签

    一.参数说明 语法 作用 Scale(window, label="滑块") 滑块标题 Scale(window, label="滑块", from_=0) 滑 ...