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 。
如果可以完成上述分割,则返回 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:
首先使用两个哈希 map:nc和tail
因此,只有检查到某个数时,这个数未被消耗完,且既不能和前面组成连续子序列,也不能和后面组成连续子序列时,无法分割
举例
以 nums=[1,2,3,3,4,4,5] 为例
nc[1] = 0、nc[2]=0、nc[3]=1、nc[4]=2、nc[5]=1 tail[3]=1
nc[1] = 0、nc[2]=0、nc[3]=0、nc[4]=1、nc[5]=0 tail[3]=1、tail[5]=1
nc[1] = 0、nc[2]=0、nc[3]=0、nc[4]=0、nc[5]=0 tail[3]=0、tail[4]=1、tail[5]=1
const isPossible = (nums) => { let max = nums[nums.length - 1] // nc:存储原数组中数字每个数字出现的次数 // tail:存储以数字num结尾的且符合题意的连续子序列个数 let nc = new Array(max + 2).fill(0), tail = new Array(max + 2).fill(0) for(let num in nums){ nc[num]++; } for(let num in nums){ if(nc[num] == 0) continue; else if(tail[num-1] > 0){ tail[num-1]--; tail[num]++; }else if(nc[num+1] > 0 && nc[num+2] > 0){ nc[num+1]--; nc[num+2]--; tail[num+2]++; }else{ return false; } nc[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: