We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 栈, 贪心, 数组, 双指针, 排序, 单调栈
给你一个整数数组 nums ,你需要找出一个 连续子数组 ,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
nums
请你找出符合题意的 最短 子数组,并输出它的长度。
示例 1:
输入:nums = [2,6,4,8,10,9,15] 输出:5 解释:你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
示例 2:
输入:nums = [1,2,3,4] 输出:0
示例 3:
输入:nums = [1] 输出:0
提示:
**进阶:**你可以设计一个时间复杂度为 O(n) 的解决方案吗?
O(n)
Language: JavaScript
/** * @param {number[]} nums * @return {number} */ var findUnsortedSubarray = function(nums) { const copied = nums.slice(0).sort((a, b) => a - b) let i = 0 let j = nums.length - 1 while (copied[i] === nums[i] && i < nums.length) { i++ } while (copied[j] === nums[j] && j >= 0) { j-- } if (i >= j) { return 0 } return j - i + 1 };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
581. 最短无序连续子数组
Description
Difficulty: 中等
Related Topics: 栈, 贪心, 数组, 双指针, 排序, 单调栈
给你一个整数数组
nums
,你需要找出一个 连续子数组 ,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。请你找出符合题意的 最短 子数组,并输出它的长度。
示例 1:
示例 2:
示例 3:
提示:
**进阶:**你可以设计一个时间复杂度为
O(n)
的解决方案吗?Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: