当前位置:网站首页>1442. number of triples forming two exclusive or equal arrays
1442. number of triples forming two exclusive or equal arrays
2022-06-11 07:02:00 【Not coriander】
Give you an array of integers arr .
Now we need to take three subscripts from the array i、j and k , among (0 <= i < j <= k < arr.length) .
a and b The definition is as follows :
a = arr[i] ^ arr[i + 1] ^ … ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ … ^ arr[k]
Be careful :^ Express Bitwise XOR operation .
Please return to can make a == b Set up a triple (i, j , k) Number of .
Example 1:
Input :arr = [2,3,1,6,7]
Output :4
explain : The triples that satisfy the meaning of the question are (0,1,2), (0,2,2), (2,3,4) as well as (2,4,4)
Example 2:
Input :arr = [1,1,1,1,1]
Output :10
Example 3:
Input :arr = [2,3]
Output :0
Example 5:
Input :arr = [7,11,12,9,5,2,7,17,22]
Output :8
Tips :
1 <= arr.length <= 300
1 <= arr[i] <= 10^8
1. Triple cycle
class Solution {
public:
int countTriplets(vector<int>& arr) {
int n=arr.size();
vector<int>s(n+1);
for(int i=0;i<n;i++){
s[i+1]=s[i]^arr[i];
}
int cnt=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j;k<n;k++){
if(s[i]==s[k+1]){
cnt++;
}
}
}
}
return cnt;
}
};
2. Double cycle
class Solution {
public:
int countTriplets(vector<int>& arr) {
int n=arr.size();
vector<int>s(n+1);
for(int i=0;i<n;i++){
s[i+1]=s[i]^arr[i];
}
int cnt=0;
for(int i=0;i<n;i++){
for(int k=i+1;k<n;k++){
if(s[i]==s[k+1]){
cnt+=(k-i);
}
}
}
return cnt;
}
};
3. Hashtable ( A heavy cycle )
class Solution {
public:
int countTriplets(vector<int>& arr) {
int n=arr.size();
vector<int>s(n+1);
for(int i=0;i<n;i++){
s[i+1]=s[i]^arr[i];
}
int cnt=0;
map<int,int>m1;
map<int,int>m2;
for(int k=0;k<n;k++){
if(m1.count(s[k+1])){
cnt+=m1[s[k+1]]*k-m2[s[k+1]];
}
m1[s[k]]++;
m2[s[k]]+=k;
}
return cnt;
}
};
4. Prefixes and optimizations
class Solution {
public:
int countTriplets(vector<int>& arr) {
int n=arr.size();
int cnt=0;
map<int,int>m1;
map<int,int>m2;
int s1=0;
int s2=0;
for(int k=0;k<n;k++){
s2=s1^arr[k];
if(m1.count(s2)){
cnt+=m1[s2]*k-m2[s2];
}
m1[s1]++;
m2[s1]+=k;
s1=s2;
}
return cnt;
}
};
边栏推荐
- webserver
- Biweekly investment and financial report: capital rush yuan universe game
- Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
- Do you use typescript or anyscript
- Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update
- Leetcode hot topic 100 topic 11-15 solution
- Quality-aware Feature Aggregation Networkfor Robust RGBT Tracking
- 关于组织开展2022年宁波市重点首版次软件申报工作的通知
- client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
- . Net C Foundation (6): namespace - scope with name
猜你喜欢

双周投融报:资本抢滩元宇宙游戏

Menu double linkage effect in uniapp
![Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]](/img/0d/2de3413fcb77ac2bd093677035f2e7.jpg)
Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]

Latex various arrows / arrows with text labels / variable length arrows

Biweekly investment and financial report: capital rush yuan universe game

你知道IT人才外派服务报价是怎样的么?建议程序员也了解下

Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)

matplotlib的cmap

生物序列智能分析平台blog(1)

Shutter restraint container assembly
随机推荐
[MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
Method to determine whether it is an array
Cross-Modal Pattern-Propagation for RGB-T Tracking
争议很大的问题
The difference between arrow function and ordinary function
通过 Ingress 进行灰度发布
数学方法论的含义和研究意义
1266_FreeRTOS调度器启动代码实现分析
Leetcode hot topic 100 topic 11-15 solution
The realization of online Fox game server room configuration battle engagement customization function
Zabbix 监控主机是否在线
AppClips&Tips(持续更新)
开源漫画服务器Mango
client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
Leetcode hot topic 100 topic 6-10 solution
Leetcode hot topic 100 topic 21-25 solution
你知道IT人才外派服务报价是怎样的么?建议程序员也了解下
Shell脚本之启动Nacos服务端
ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
Starting from scratch (V) realize bullet positioning and animation