581. Shortest Unsorted Continuous Subarray
一、题目
1、审题
2、分析
给定一个无序整形数组,将其变为有序,最少要将连续的多少个元素进行排序。
二、解答
1、思路
① 采用两个指针。 begin 记录需要排序的元素的最低下标; end 记录需要排序的元素的最高下标。
② 顺序遍历数组, 采用变量 max,记录到当前元素为止的最大元素值,若当前元素值 < max ,即此元素需要进行重新排序。采用 end 记录。
③ 逆序遍历数组,采用变量 min,记录到当前元素为止的最小元素值,若当前元素值 > min,即词元素值也需要重新怕徐。采用 begin 记录。
④ 最终需要排序的元素下标序列为 begin ~ end 。 总元素个数为 end - begin +1;
public int findUnsortedSubarray(int[] nums) {int n = nums.length;int begin = -1, end = -2; // 若数组已经有序,则最终的结果 end - begin + 1 == 0int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;// begin 比 min 大, end 比 max 小// 找到最后的一个元素, 这个元素从左看比最大的石头小(end 后面的都比最大的数值大)for (int i = 0; i < n; i++) {max = Math.max(max, nums[i]);if(nums[i] < max)end = i;}// 找到最前面的一个元素,这个元素从右看比最小的石头大。(begin 前面的都比最下的数值小)for (int i = n - 1; i >= 0; i--) {min = Math.min(min, nums[i]);if(nums[i] > min)begin = i;}return end - begin + 1;}
转载于:https://www.cnblogs.com/skillking/p/10935940.html
581. Shortest Unsorted Continuous Subarray相关推荐
- 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
题目: Given an integer array, you need to find one continuous subarray that if you only sort this suba ...
- 【leetcode】581. Shortest Unsorted Continuous Subarray
题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /*** @param {number[]} nums* @return ...
- leetcode 581. Shortest Unsorted Continuous Subarray | 581. 最短无序连续子数组(单调栈)
题目 https://leetcode.com/problems/shortest-unsorted-continuous-subarray/ 题解 乍一看,没思路,直奔 Related Topics ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- C#LeetCode刷题之#581-最短无序连续子数组( Shortest Unsorted Continuous Subarray)
问题 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 输入: [2, 6, 4, 8, 10, ...
- LeetCode Shortest Unsorted Continuous Subarray
题意:给出一个数组,求其最小的无序数组,这个无序数组经过排序后,整个数据也是排好序的 思路:将数据排序,然后与原来的数组比较,从开始找到不相等的位置,然后从尾部开始找到不相等的位置,两个位置差就是最小 ...
- 28/100. Shortest Unsorted Continuous Subarray
给定一个整数数组nums,需要找到一个连续的子数组,该子数组满足条件:如果只按升序对这个子数组排序,那么整个数组也将按升序排序.返回子数组的长度. 对nums进行排序,生成一个新数组newNums,需 ...
- leetcode-581-Shortest Unsorted Continuous Subarray
题目描述: Given an integer array, you need to find one continuous subarray that if you only sort this su ...
- LeetCode之Shortest Unsorted Continous Subarray
题目大意:将给定的数组从小到大排序,找出最短的需要进行位置变换的子数组的长度 1.找到子数组起始的位置和结束位置 2.最小长度=结束位置-起始位置+1 public static int findUn ...
最新文章
- 聊一聊2020年实例分割领域的进展和未来展望
- android定义空字符串数组,android – retrofit:处理可以是空字符串或数组的属性
- JVM启动参数解析(转)
- AFNetworking post请求遇到问题解决过程
- Kafka【入门】就这一篇!
- win7访问linux共享路径不存在,win7系统访问网络共享找不到网络路径如何解决
- dll侧加载_WORD打开时出现加载DLL错误解决方法
- MyBatis 如何传递参数(全)
- 如何用jquery实现实时监控浏览器宽度
- HTML中的表格和表单控件详解
- 求一个截取字符的正则表达式
- 删除下拉框只找23火星软件_用Rhino秀个火星榨汁机?
- react中一个音频或视频播放的时候其他音视频暂停播放
- 不知名程序员金山云大米云主机体验日志
- 扫码枪连接mysql教学_Android PDA扫描枪广播接搜条码并使用
- (L)小写l和(i)大写I的区分方法
- Android Q - 设置默认输入法
- Android Studio 插件-Android Styler 的使用 (转)
- bufferedimage 保存图片_如何提取图片中的文字
- 【附源码】Java计算机毕业设计安卓高速铁路配餐app论文(程序+LW+部署)