Skip to content

Commit 5f268ca

Browse files
authored
Create 2588.Count-the-Number-of-Beautiful-Subarrays.cpp
1 parent 04a9575 commit 5f268ca

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
long long beautifulSubarrays(vector<int>& nums)
4+
{
5+
unordered_map<int,long long>Map;
6+
Map[0] = 1;
7+
int state = 0;
8+
long long ret = 0;
9+
for (int i=0; i<nums.size(); i++)
10+
{
11+
int x = nums[i];
12+
for (int k=0; k<31; k++)
13+
{
14+
int t = ((x>>k)&1) + ((state>>k)&1);
15+
t = t%2;
16+
state = state - (((state>>k)&1)<<k) + (t<<k);
17+
}
18+
ret += Map[state];
19+
Map[state] += 1;
20+
}
21+
22+
return ret;
23+
24+
}
25+
};

0 commit comments

Comments
 (0)