Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f563c72

Browse files
feat: add solutions to lc problem: No.1442 (doocs#4267)
No.1442.Count Triplets That Can Form Two Arrays of Equal XOR
1 parent 6793e6d commit f563c72

File tree

8 files changed

+217
-149
lines changed

8 files changed

+217
-149
lines changed

‎solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README.md‎

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ tags:
8585

8686
<!-- solution:start -->
8787

88-
### 方法一
88+
### 方法一:枚举
89+
90+
根据题目描述,要找到满足 $a = b$ 的三元组 $(i, j, k),ドル即满足 $s = a \oplus b = 0,ドル我们只需要枚举左端点 $i,ドル然后计算以 $k$ 为右端点的区间 $[i, k]$ 的前缀异或和 $s,ドル如果 $s = 0,ドル那么对于任意 $j \in [i + 1, k],ドル都满足 $a = b,ドル即 $(i, j, k)$ 是一个满足条件的三元组,一共有 $k - i$ 个,我们将其累加到答案中即可。
91+
92+
枚举结束后,返回答案即可。
93+
94+
时间复杂度 $O(n^2),ドル其中 $n$ 是数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。
8995

9096
<!-- tabs:start -->
9197

@@ -94,17 +100,13 @@ tags:
94100
```python
95101
class Solution:
96102
def countTriplets(self, arr: List[int]) -> int:
97-
n = len(arr)
98-
pre = [0] * (n + 1)
99-
for i in range(n):
100-
pre[i + 1] = pre[i] ^ arr[i]
101-
ans = 0
102-
for i in range(n - 1):
103-
for j in range(i + 1, n):
104-
for k in range(j, n):
105-
a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j]
106-
if a == b:
107-
ans += 1
103+
ans, n = 0, len(arr)
104+
for i, x in enumerate(arr):
105+
s = x
106+
for k in range(i + 1, n):
107+
s ^= arr[k]
108+
if s == 0:
109+
ans += k - i
108110
return ans
109111
```
110112

@@ -113,20 +115,13 @@ class Solution:
113115
```java
114116
class Solution {
115117
public int countTriplets(int[] arr) {
116-
int n = arr.length;
117-
int[] pre = new int[n + 1];
118+
int ans = 0, n = arr.length;
118119
for (int i = 0; i < n; ++i) {
119-
pre[i + 1] = pre[i] ^ arr[i];
120-
}
121-
int ans = 0;
122-
for (int i = 0; i < n - 1; ++i) {
123-
for (int j = i + 1; j < n; ++j) {
124-
for (int k = j; k < n; ++k) {
125-
int a = pre[j] ^ pre[i];
126-
int b = pre[k + 1] ^ pre[j];
127-
if (a == b) {
128-
++ans;
129-
}
120+
int s = arr[i];
121+
for (int k = i + 1; k < n; ++k) {
122+
s ^= arr[k];
123+
if (s == 0) {
124+
ans += k - i;
130125
}
131126
}
132127
}
@@ -141,15 +136,13 @@ class Solution {
141136
class Solution {
142137
public:
143138
int countTriplets(vector<int>& arr) {
144-
int n = arr.size();
145-
vector<int> pre(n + 1);
146-
for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i];
147-
int ans = 0;
148-
for (int i = 0; i < n - 1; ++i) {
149-
for (int j = i + 1; j < n; ++j) {
150-
for (int k = j; k < n; ++k) {
151-
int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j];
152-
if (a == b) ++ans;
139+
int ans = 0, n = arr.size();
140+
for (int i = 0; i < n; ++i) {
141+
int s = arr[i];
142+
for (int k = i + 1; k < n; ++k) {
143+
s ^= arr[k];
144+
if (s == 0) {
145+
ans += k - i;
153146
}
154147
}
155148
}
@@ -161,24 +154,59 @@ public:
161154
#### Go
162155
163156
```go
164-
func countTriplets(arr []int) int {
165-
n := len(arr)
166-
pre := make([]int, n+1)
167-
for i := 0; i < n; i++ {
168-
pre[i+1] = pre[i] ^ arr[i]
169-
}
170-
ans := 0
171-
for i := 0; i < n-1; i++ {
172-
for j := i + 1; j < n; j++ {
173-
for k := j; k < n; k++ {
174-
a, b := pre[j]^pre[i], pre[k+1]^pre[j]
175-
if a == b {
176-
ans++
177-
}
157+
func countTriplets(arr []int) (ans int) {
158+
for i, x := range arr {
159+
s := x
160+
for k := i + 1; k < len(arr); k++ {
161+
s ^= arr[k]
162+
if s == 0 {
163+
ans += k - i
178164
}
179165
}
180166
}
181-
return ans
167+
return
168+
}
169+
```
170+
171+
#### TypeScript
172+
173+
```ts
174+
function countTriplets(arr: number[]): number {
175+
const n = arr.length;
176+
let ans = 0;
177+
for (let i = 0; i < n; ++i) {
178+
let s = arr[i];
179+
for (let k = i + 1; k < n; ++k) {
180+
s ^= arr[k];
181+
if (s === 0) {
182+
ans += k - i;
183+
}
184+
}
185+
}
186+
return ans;
187+
}
188+
```
189+
190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn count_triplets(arr: Vec<i32>) -> i32 {
195+
let mut ans = 0;
196+
let n = arr.len();
197+
198+
for i in 0..n {
199+
let mut s = arr[i];
200+
for k in (i + 1)..n {
201+
s ^= arr[k];
202+
if s == 0 {
203+
ans += (k - i) as i32;
204+
}
205+
}
206+
}
207+
208+
ans
209+
}
182210
}
183211
```
184212

‎solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README_EN.md‎

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Enumeration
71+
72+
According to the problem description, to find triplets $(i, j, k)$ that satisfy $a = b,ドル which means $s = a \oplus b = 0,ドル we only need to enumerate the left endpoint $i,ドル and then calculate the prefix XOR sum $s$ of the interval $[i, k]$ with $k$ as the right endpoint. If $s = 0,ドル then for any $j \in [i + 1, k],ドル the condition $a = b$ is satisfied, meaning $(i, j, k)$ is a valid triplet. There are $k - i$ such triplets, which we can add to our answer.
73+
74+
After the enumeration is complete, we return the answer.
75+
76+
The time complexity is $O(n^2),ドル where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$.
7177

7278
<!-- tabs:start -->
7379

@@ -76,17 +82,13 @@ tags:
7682
```python
7783
class Solution:
7884
def countTriplets(self, arr: List[int]) -> int:
79-
n = len(arr)
80-
pre = [0] * (n + 1)
81-
for i in range(n):
82-
pre[i + 1] = pre[i] ^ arr[i]
83-
ans = 0
84-
for i in range(n - 1):
85-
for j in range(i + 1, n):
86-
for k in range(j, n):
87-
a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j]
88-
if a == b:
89-
ans += 1
85+
ans, n = 0, len(arr)
86+
for i, x in enumerate(arr):
87+
s = x
88+
for k in range(i + 1, n):
89+
s ^= arr[k]
90+
if s == 0:
91+
ans += k - i
9092
return ans
9193
```
9294

@@ -95,20 +97,13 @@ class Solution:
9597
```java
9698
class Solution {
9799
public int countTriplets(int[] arr) {
98-
int n = arr.length;
99-
int[] pre = new int[n + 1];
100+
int ans = 0, n = arr.length;
100101
for (int i = 0; i < n; ++i) {
101-
pre[i + 1] = pre[i] ^ arr[i];
102-
}
103-
int ans = 0;
104-
for (int i = 0; i < n - 1; ++i) {
105-
for (int j = i + 1; j < n; ++j) {
106-
for (int k = j; k < n; ++k) {
107-
int a = pre[j] ^ pre[i];
108-
int b = pre[k + 1] ^ pre[j];
109-
if (a == b) {
110-
++ans;
111-
}
102+
int s = arr[i];
103+
for (int k = i + 1; k < n; ++k) {
104+
s ^= arr[k];
105+
if (s == 0) {
106+
ans += k - i;
112107
}
113108
}
114109
}
@@ -123,15 +118,13 @@ class Solution {
123118
class Solution {
124119
public:
125120
int countTriplets(vector<int>& arr) {
126-
int n = arr.size();
127-
vector<int> pre(n + 1);
128-
for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i];
129-
int ans = 0;
130-
for (int i = 0; i < n - 1; ++i) {
131-
for (int j = i + 1; j < n; ++j) {
132-
for (int k = j; k < n; ++k) {
133-
int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j];
134-
if (a == b) ++ans;
121+
int ans = 0, n = arr.size();
122+
for (int i = 0; i < n; ++i) {
123+
int s = arr[i];
124+
for (int k = i + 1; k < n; ++k) {
125+
s ^= arr[k];
126+
if (s == 0) {
127+
ans += k - i;
135128
}
136129
}
137130
}
@@ -143,24 +136,59 @@ public:
143136
#### Go
144137
145138
```go
146-
func countTriplets(arr []int) int {
147-
n := len(arr)
148-
pre := make([]int, n+1)
149-
for i := 0; i < n; i++ {
150-
pre[i+1] = pre[i] ^ arr[i]
151-
}
152-
ans := 0
153-
for i := 0; i < n-1; i++ {
154-
for j := i + 1; j < n; j++ {
155-
for k := j; k < n; k++ {
156-
a, b := pre[j]^pre[i], pre[k+1]^pre[j]
157-
if a == b {
158-
ans++
159-
}
139+
func countTriplets(arr []int) (ans int) {
140+
for i, x := range arr {
141+
s := x
142+
for k := i + 1; k < len(arr); k++ {
143+
s ^= arr[k]
144+
if s == 0 {
145+
ans += k - i
160146
}
161147
}
162148
}
163-
return ans
149+
return
150+
}
151+
```
152+
153+
#### TypeScript
154+
155+
```ts
156+
function countTriplets(arr: number[]): number {
157+
const n = arr.length;
158+
let ans = 0;
159+
for (let i = 0; i < n; ++i) {
160+
let s = arr[i];
161+
for (let k = i + 1; k < n; ++k) {
162+
s ^= arr[k];
163+
if (s === 0) {
164+
ans += k - i;
165+
}
166+
}
167+
}
168+
return ans;
169+
}
170+
```
171+
172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn count_triplets(arr: Vec<i32>) -> i32 {
177+
let mut ans = 0;
178+
let n = arr.len();
179+
180+
for i in 0..n {
181+
let mut s = arr[i];
182+
for k in (i + 1)..n {
183+
s ^= arr[k];
184+
if s == 0 {
185+
ans += (k - i) as i32;
186+
}
187+
}
188+
}
189+
190+
ans
191+
}
164192
}
165193
```
166194

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
22
public:
33
int countTriplets(vector<int>& arr) {
4-
int n = arr.size();
5-
vector<int> pre(n + 1);
6-
for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i];
7-
int ans = 0;
8-
for (int i = 0; i < n - 1; ++i) {
9-
for (int j = i + 1; j < n; ++j) {
10-
for (int k = j; k < n; ++k) {
11-
int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j];
12-
if (a == b) ++ans;
4+
int ans = 0, n = arr.size();
5+
for (int i = 0; i < n; ++i) {
6+
int s = arr[i];
7+
for (int k = i + 1; k < n; ++k) {
8+
s ^= arr[k];
9+
if (s == 0) {
10+
ans += k - i;
1311
}
1412
}
1513
}
1614
return ans;
1715
}
18-
};
16+
};
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
func countTriplets(arr []int) int {
2-
n := len(arr)
3-
pre := make([]int, n+1)
4-
for i := 0; i < n; i++ {
5-
pre[i+1] = pre[i] ^ arr[i]
6-
}
7-
ans := 0
8-
for i := 0; i < n-1; i++ {
9-
for j := i + 1; j < n; j++ {
10-
for k := j; k < n; k++ {
11-
a, b := pre[j]^pre[i], pre[k+1]^pre[j]
12-
if a == b {
13-
ans++
14-
}
1+
func countTriplets(arr []int) (ans int) {
2+
for i, x := range arr {
3+
s := x
4+
for k := i + 1; k < len(arr); k++ {
5+
s ^= arr[k]
6+
if s == 0 {
7+
ans += k - i
158
}
169
}
1710
}
18-
returnans
19-
}
11+
return
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /