Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2024-12-09 21:35:47
3152. Special Array II
去用prefix_sum纪录有几组奇偶对数
如果nums[i]跟nums[i-1]奇偶性不同
那prefix_sum[i]=prefix_sum[i-1]+1
ex [1,2,3,4,5,6]
prefix_sum=[0,1,2,3,4,5]
接着就去看每组queries内有没有足够的奇偶对数
假设from=1、to=4
那这样要有3组奇偶对才会是true
golang code :
func isArraySpecial(nums []int, queries [][]int) []bool {
n:=len(queries)
res:=make([]bool,n)
prefix:=make([]int,len(nums))
for i:=1;i<len(nums);i++{
if (nums[i]+nums[i-1]) & 1==1{
prefix[i]=1
}
prefix[i]+=prefix[i-1]
}
for key,val:=range queries{
tmp:=val[1]-val[0]
if prefix[val[1]]-prefix[val[0]]==tmp{
res[key]=true
}
}
return res
}
作者: sustainer123 (caster)   2024-12-09 21:37:00
大师
作者: Furina (芙宁娜)   2024-12-09 21:45:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com