-
Notifications
You must be signed in to change notification settings - Fork 0
/
1442_countTriplets.txt
40 lines (39 loc) · 1.01 KB
/
1442_countTriplets.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int countTriplets(vector<int>& arr) {
//前缀异或和
int n = arr.size();
int res = 0;
vector<int> prexor(n+1);
for(int i = 0;i<n;i++)
prexor[i+1] = prexor[i] ^ arr[i];
for(int i = 1;i<=n;i++){
for(int j = i+1;j<=n;j++){
for(int k = j;k<=n;k++){
int a = prexor[i-1] ^ prexor[j-1];
int b = prexor[k] ^ prexor[j-1];
res += (a==b);
}
}
}
return res;
}
};
class Solution {
public:
int countTriplets(vector<int>& arr) {
//前缀异或和
int n = arr.size();
int res = 0;
vector<int> prexor(n+1);
for(int i = 0;i<n;i++)
prexor[i+1] = prexor[i] ^ arr[i];
for(int i = 1;i<=n;i++){
for(int k = i+1;k<=n;k++){
if(prexor[i-1]==prexor[k])
res += k-i;
}
}
return res;
}
};