# 2588. Count the Number of Beautiful Subarrays ###### tags: `Leetcode` `Medium` `Prefix Sum` `Bit Manipulation` `XOR` Link: https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/description/ ## 思路 如果我们统计每个bit里 1出现的次数 满足条件的subarray需要满足每个次数都是偶数 进一步 也就是这个subarray所有数字xor在一起 结果是0 ## Code ```python= class Solution: def beautifulSubarrays(self, nums: List[int]) -> int: freq = Counter({0:1}) ans = prefix = 0 for num in nums: prefix ^= num ans += freq[prefix] freq[prefix] += 1 return ans ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up