【leetcode_easy】581. Shortest Unsorted Continuous Subarray
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;
4. c++ O(n) one-pass solution which beats almost;
完
【leetcode_easy】581. Shortest Unsorted Continuous Subarray的更多相关文章
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- 【leetcode】581. Shortest Unsorted Continuous Subarray
题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /** * @param {number[]} nums * @retur ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- 581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...
- 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况
[抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- Shortest Unsorted Continuous Subarray LT581
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [Swift]LeetCode581. 最短无序连续子数组 | Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
随机推荐
- SpringBoot项目mysql配置文件密码加密(jasypt)
起因:因为我个人微博想要公开源码,但数据库配置文件会暴露在外面,又不想生产跟开发环境建来回切换,所以想到了加密数据库密码,于是问了群里的一个朋友小XX(原谅我不识字) 经过: 简单粗暴,直接上代码: ...
- shell 学习笔记 LinuxShell脚本攻略(第2版)
注释用#号:多条命令通过分号或回车来分隔 echo会自动换行,若不想换行,则加上-n参数,如 echo -n "nice to meet you" echo后面的内容可以不带引号, ...
- MySQL 表之间的关系
表之间的关系 # 定义一张部门员工表 emp id name gander dep_name dep_desc 1 ming male 教学部 教书 2 lilei male 教学部 教书 3 ham ...
- Django --- 路由层(urls)
目录 1.orm表关系如何建立 2.django请求生命周期流程图 3.urls.py路由层 4.路由匹配 5.无名分组 6.有名分组 7.反向解析 8.路由分发 9.名称空间 10.伪静态 11.虚 ...
- js与json的区别,json的概述,json与面向对象,json与对象的转换
<script> //js与json的区别,json的概述,json与面向对象,json与对象的转换 //json的概述:json(javascript object Notation,j ...
- Kubernetes的YAML文件
deployments: - apiVersion: apps/v1beta1 kind: Deployment metadata: labels: system_serviceUnit: bas-b ...
- HTML div块内剧中
在HTML中有一个块内元素剧中的方法 那就是margin:0 auto; 剧中前 剧中后
- 十六.部署PXE网络装机
PXE组件及过程分析 • 需要哪些服务组件? – DHCP服务,分配IP地址.定位引导程序 – TFTP服务,提供引导程序下载 – HTTP服务,提供yum安装源 • 客户机应具备的条件 – 网卡芯片 ...
- P2679 子串 DP
P2679 子串 DP 从字符串A中取出\(k\)段子串,按原顺序拼接,问存在多少个方案使拼接的字符串与字符串B相同 淦,又是这种字符串dp 设状态\(ans[i][j][k]\)表示A串位置\(i\ ...
- PHP全栈学习笔记26
php 验证码 <?php /* *@Author: 达叔小生 **/ header("Content-type:image/png"); // 发送头部信息,生成png图片 ...