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 e84c011

Browse files
committed
feat: add solutions to lc problem: No.1442
No.1442.Count Triplets That Can Form Two Arrays of Equal XOR
1 parent 5b0cd14 commit e84c011

File tree

8 files changed

+257
-38
lines changed

8 files changed

+257
-38
lines changed

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

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,112 @@
6363
<li><code>1 &lt;= arr[i] &lt;= 10^8</code></li>
6464
</ul>
6565

66-
6766
## 解法
6867

6968
<!-- 这里可写通用的实现逻辑 -->
7069

70+
前缀异或,然后暴力枚举即可。
71+
7172
<!-- tabs:start -->
7273

7374
### **Python3**
7475

7576
<!-- 这里可写当前语言的特殊实现逻辑 -->
7677

7778
```python
78-
79+
class Solution:
80+
def countTriplets(self, arr: List[int]) -> int:
81+
n = len(arr)
82+
pre = [0] * (n + 1)
83+
for i in range(n):
84+
pre[i + 1] = pre[i] ^ arr[i]
85+
ans = 0
86+
for i in range(n - 1):
87+
for j in range(i + 1, n):
88+
for k in range(j, n):
89+
a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j]
90+
if a == b:
91+
ans += 1
92+
return ans
7993
```
8094

8195
### **Java**
8296

8397
<!-- 这里可写当前语言的特殊实现逻辑 -->
8498

8599
```java
100+
class Solution {
101+
public int countTriplets(int[] arr) {
102+
int n = arr.length;
103+
int[] pre = new int[n + 1];
104+
for (int i = 0; i < n; ++i) {
105+
pre[i + 1] = pre[i] ^ arr[i];
106+
}
107+
int ans = 0;
108+
for (int i = 0; i < n - 1; ++i) {
109+
for (int j = i + 1; j < n; ++j) {
110+
for (int k = j; k < n; ++k) {
111+
int a = pre[j] ^ pre[i];
112+
int b = pre[k + 1] ^ pre[j];
113+
if (a == b) {
114+
++ans;
115+
}
116+
}
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int countTriplets(vector<int>& arr) {
130+
int n = arr.size();
131+
vector<int> pre(n + 1);
132+
for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i];
133+
int ans = 0;
134+
for (int i = 0; i < n - 1; ++i)
135+
{
136+
for (int j = i + 1; j < n; ++j)
137+
{
138+
for (int k = j; k < n; ++k)
139+
{
140+
int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j];
141+
if (a == b) ++ans;
142+
}
143+
}
144+
}
145+
return ans;
146+
}
147+
};
148+
```
86149
150+
### **Go**
151+
152+
```go
153+
func countTriplets(arr []int) int {
154+
n := len(arr)
155+
pre := make([]int, n+1)
156+
for i := 0; i < n; i++ {
157+
pre[i+1] = pre[i] ^ arr[i]
158+
}
159+
ans := 0
160+
for i := 0; i < n-1; i++ {
161+
for j := i + 1; j < n; j++ {
162+
for k := j; k < n; k++ {
163+
a, b := pre[j]^pre[i], pre[k+1]^pre[j]
164+
if a == b {
165+
ans++
166+
}
167+
}
168+
}
169+
}
170+
return ans
171+
}
87172
```
88173

89174
### **...**

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

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,23 @@
66

77
<p>Given an array of&nbsp;integers <code>arr</code>.</p>
88

9-
10-
119
<p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)</code>.</p>
1210

13-
14-
1511
<p>Let&#39;s define <code>a</code> and <code>b</code> as follows:</p>
1612

17-
18-
1913
<ul>
2014
<li><code>a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]</code></li>
2115
<li><code>b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]</code></li>
2216
</ul>
2317

24-
25-
2618
<p>Note that <strong>^</strong> denotes the <strong>bitwise-xor</strong> operation.</p>
2719

28-
29-
3020
<p>Return <em>the number of triplets</em> (<code>i</code>, <code>j</code> and <code>k</code>) Where <code>a == b</code>.</p>
3121

32-
33-
3422
<p>&nbsp;</p>
3523

3624
<p><strong>Example 1:</strong></p>
3725

38-
39-
4026
<pre>
4127

4228
<strong>Input:</strong> arr = [2,3,1,6,7]
@@ -47,12 +33,8 @@
4733

4834
</pre>
4935

50-
51-
5236
<p><strong>Example 2:</strong></p>
5337

54-
55-
5638
<pre>
5739

5840
<strong>Input:</strong> arr = [1,1,1,1,1]
@@ -61,12 +43,8 @@
6143

6244
</pre>
6345

64-
65-
6646
<p><strong>Example 3:</strong></p>
6747

68-
69-
7048
<pre>
7149

7250
<strong>Input:</strong> arr = [2,3]
@@ -75,12 +53,8 @@
7553

7654
</pre>
7755

78-
79-
8056
<p><strong>Example 4:</strong></p>
8157

82-
83-
8458
<pre>
8559

8660
<strong>Input:</strong> arr = [1,3,5,7,9]
@@ -89,12 +63,8 @@
8963

9064
</pre>
9165

92-
93-
9466
<p><strong>Example 5:</strong></p>
9567

96-
97-
9868
<pre>
9969

10070
<strong>Input:</strong> arr = [7,11,12,9,5,2,7,17,22]
@@ -103,14 +73,10 @@
10373

10474
</pre>
10575

106-
107-
10876
<p>&nbsp;</p>
10977

11078
<p><strong>Constraints:</strong></p>
11179

112-
113-
11480
<ul>
11581
<li><code>1 &lt;= arr.length &lt;= 300</code></li>
11682
<li><code>1 &lt;= arr[i] &lt;= 10^8</code></li>
@@ -123,13 +89,97 @@
12389
### **Python3**
12490

12591
```python
126-
92+
class Solution:
93+
def countTriplets(self, arr: List[int]) -> int:
94+
n = len(arr)
95+
pre = [0] * (n + 1)
96+
for i in range(n):
97+
pre[i + 1] = pre[i] ^ arr[i]
98+
ans = 0
99+
for i in range(n - 1):
100+
for j in range(i + 1, n):
101+
for k in range(j, n):
102+
a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j]
103+
if a == b:
104+
ans += 1
105+
return ans
127106
```
128107

129108
### **Java**
130109

131110
```java
111+
class Solution {
112+
public int countTriplets(int[] arr) {
113+
int n = arr.length;
114+
int[] pre = new int[n + 1];
115+
for (int i = 0; i < n; ++i) {
116+
pre[i + 1] = pre[i] ^ arr[i];
117+
}
118+
int ans = 0;
119+
for (int i = 0; i < n - 1; ++i) {
120+
for (int j = i + 1; j < n; ++j) {
121+
for (int k = j; k < n; ++k) {
122+
int a = pre[j] ^ pre[i];
123+
int b = pre[k + 1] ^ pre[j];
124+
if (a == b) {
125+
++ans;
126+
}
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int countTriplets(vector<int>& arr) {
141+
int n = arr.size();
142+
vector<int> pre(n + 1);
143+
for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i];
144+
int ans = 0;
145+
for (int i = 0; i < n - 1; ++i)
146+
{
147+
for (int j = i + 1; j < n; ++j)
148+
{
149+
for (int k = j; k < n; ++k)
150+
{
151+
int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j];
152+
if (a == b) ++ans;
153+
}
154+
}
155+
}
156+
return ans;
157+
}
158+
};
159+
```
132160
161+
### **Go**
162+
163+
```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+
}
178+
}
179+
}
180+
}
181+
return ans
182+
}
133183
```
134184

135185
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
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+
{
10+
for (int j = i + 1; j < n; ++j)
11+
{
12+
for (int k = j; k < n; ++k)
13+
{
14+
int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j];
15+
if (a == b) ++ans;
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
15+
}
16+
}
17+
}
18+
return ans
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int countTriplets(int[] arr) {
3+
int n = arr.length;
4+
int[] pre = new int[n + 1];
5+
for (int i = 0; i < n; ++i) {
6+
pre[i + 1] = pre[i] ^ arr[i];
7+
}
8+
int ans = 0;
9+
for (int i = 0; i < n - 1; ++i) {
10+
for (int j = i + 1; j < n; ++j) {
11+
for (int k = j; k < n; ++k) {
12+
int a = pre[j] ^ pre[i];
13+
int b = pre[k + 1] ^ pre[j];
14+
if (a == b) {
15+
++ans;
16+
}
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
}

0 commit comments

Comments
(0)

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