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
给你一个按升序排序的整数数组 num(可能包含重复数字),请你将它们分割成一个或多个子序列,其中每个子序列都由连续整数组成且长度至少为 3 。
num
如果可以完成上述分割,则返回 true ;否则,返回 false 。
true
false
示例 1:
输入: [1,2,3,3,4,5] 输出: True 解释: 你可以分割出这样两个连续子序列 : 1, 2, 3 3, 4, 5
示例 2:
输入: [1,2,3,3,4,4,5,5] 输出: True 解释: 你可以分割出这样两个连续子序列 : 1, 2, 3, 4, 5 3, 4, 5
示例 3:
输入: [1,2,3,4,4,5] 输出: False
提示:
leetcode
The text was updated successfully, but these errors were encountered:
从头开始,我们每次仅仅寻找满足条件的序列(连续子序列长度为3),剔除之后,依次往后遍历:
const isPossible = function(nums) { let max = nums[nums.length - 1] // arr:存储原数组中数字每个数字出现的次数 // tail:存储以数字num结尾的且符合题意的连续子序列个数 let arr = new Array(max + 2).fill(0), tail = new Array(max + 2).fill(0) for(let num of nums) { arr[num] ++ } for(let num of nums) { if(arr[num] === 0) continue else if(tail[num-1] > 0){ tail[num-1]-- tail[num]++ }else if(arr[num+1] > 0 && arr[num+2] > 0){ arr[num+1]-- arr[num+2]-- tail[num+2]++ } else { return false } arr[num]-- } return true }
复杂度分析:
Sorry, something went wrong.
No branches or pull requests
给你一个按升序排序的整数数组
num
(可能包含重复数字),请你将它们分割成一个或多个子序列,其中每个子序列都由连续整数组成且长度至少为 3 。如果可以完成上述分割,则返回
true
;否则,返回false
。示例 1:
示例 2:
示例 3:
提示:
leetcode
The text was updated successfully, but these errors were encountered: