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 43a0d90

Browse files
feat: add solutions to lc problem: No.3199 (doocs#3168)
No.3199.Count Triplets with Even XOR Set Bits I
1 parent ba957c2 commit 43a0d90

File tree

18 files changed

+662
-23
lines changed

18 files changed

+662
-23
lines changed

‎solution/1000-1099/1048.Longest String Chain/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- 双指针
1111
- 字符串
1212
- 动态规划
13+
- 排序
1314
---
1415

1516
<!-- problem:start -->

‎solution/1000-1099/1048.Longest String Chain/README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- Two Pointers
1111
- String
1212
- Dynamic Programming
13+
- Sorting
1314
---
1415

1516
<!-- problem:start -->

‎solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README.md‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ tags:
2222

2323
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code>,以及一个正整数 <code>k</code> 。</p>
2424

25-
<p>将这个数组划分为一个或多个长度为 <code>3</code> 的子数组,并满足以下条件:</p>
25+
<p>将这个数组划分为&nbsp;<code>n / 3</code>&nbsp;个长度为 <code>3</code> 的子数组,并满足以下条件:</p>
2626

2727
<ul>
28-
<li><code>nums</code> 中的 <strong>每个 </strong>元素都必须 <strong>恰好 </strong>存在于某个子数组中。</li>
29-
<li>子数组中<strong> 任意 </strong>两个元素的差必须小于或等于 <code>k</code> 。</li>
28+
<li>子数组中<strong> 任意 </strong>两个元素的差必须 <strong>小于或等于</strong> <code>k</code> 。</li>
3029
</ul>
3130

3231
<p>返回一个<em> </em><strong>二维数组 </strong>,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 <strong>任意一个</strong> 即可。</p>
@@ -35,21 +34,46 @@ tags:
3534

3635
<p><strong class="example">示例 1:</strong></p>
3736

38-
<pre>
39-
<strong>输入:</strong>nums = [1,3,4,8,7,9,3,5,1], k = 2
40-
<strong>输出:</strong>[[1,1,3],[3,4,5],[7,8,9]]
41-
<strong>解释:</strong>可以将数组划分为以下子数组:[1,1,3],[3,4,5] 和 [7,8,9] 。
42-
每个子数组中任意两个元素的差都小于或等于 2 。
43-
注意,元素的顺序并不重要。
44-
</pre>
37+
<div class="example-block">
38+
<p><span class="example-io"><b>输入:</b>nums = [1,3,4,8,7,9,3,5,1], k = 2</span></p>
39+
40+
<p><span class="example-io"><b>输出:</b>[[1,1,3],[3,4,5],[7,8,9]]</span></p>
41+
42+
<p><strong>解释:</strong></p>
43+
44+
<p>每个数组中任何两个元素之间的差小于或等于 2。</p>
45+
</div>
4546

4647
<p><strong class="example">示例 2:</strong></p>
4748

48-
<pre>
49-
<strong>输入:</strong>nums = [1,3,3,2,7,3], k = 3
50-
<strong>输出:</strong>[]
51-
<strong>解释:</strong>无法划分数组满足所有条件。
52-
</pre>
49+
<div class="example-block">
50+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">nums = [2,4,2,2,5,2], k = 2</span></p>
51+
52+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">[]</span></p>
53+
54+
<p><strong>解释:</strong></p>
55+
56+
<p>将&nbsp;<code>nums</code>&nbsp;划分为 2 个长度为 3 的数组的不同方式有:</p>
57+
58+
<ul>
59+
<li>[[2,2,2],[2,4,5]] (及其排列)</li>
60+
<li>[[2,2,4],[2,2,5]] (及其排列)</li>
61+
</ul>
62+
63+
<p>因为有四个 2,所以无论我们如何划分,都会有一个包含元素 2 和 5 的数组。因为&nbsp;<code>5 - 2 = 3 &gt; k</code>,条件无法被满足,所以没有合法的划分。</p>
64+
</div>
65+
66+
<p><strong class="example">示例 3:</strong></p>
67+
68+
<div class="example-block">
69+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14</span></p>
70+
71+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">[[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]</span></p>
72+
73+
<p><strong>解释:</strong></p>
74+
75+
<p>每个数组中任何两个元素之间的差小于或等于 14。</p>
76+
</div>
5377

5478
<p>&nbsp;</p>
5579

‎solution/3100-3199/3195.Find the Minimum Area to Cover All Ones I/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/README.md
5+
tags:
6+
- 数组
7+
- 矩阵
58
---
69

710
<!-- problem:start -->

‎solution/3100-3199/3195.Find the Minimum Area to Cover All Ones I/README_EN.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/README_EN.md
5+
tags:
6+
- Array
7+
- Matrix
58
---
69

710
<!-- problem:start -->

‎solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3196.Maximize%20Total%20Cost%20of%20Alternating%20Subarrays/README.md
5+
tags:
6+
- 数组
7+
- 动态规划
58
---
69

710
<!-- problem:start -->

‎solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README_EN.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3196.Maximize%20Total%20Cost%20of%20Alternating%20Subarrays/README_EN.md
5+
tags:
6+
- Array
7+
- Dynamic Programming
58
---
69

710
<!-- problem:start -->

‎solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/README.md
5+
tags:
6+
- 数组
7+
- 枚举
8+
- 矩阵
59
---
610

711
<!-- problem:start -->

‎solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README_EN.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/README_EN.md
5+
tags:
6+
- Array
7+
- Enumeration
8+
- Matrix
59
---
610

711
<!-- problem:start -->
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3199.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3199. Count Triplets with Even XOR Set Bits I 🔒](https://leetcode.cn/problems/count-triplets-with-even-xor-set-bits-i)
10+
11+
[English Version](/solution/3100-3199/3199.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
Given three integer arrays <code>a</code>, <code>b</code>, and <code>c</code>, return the number of triplets <code>(a[i], b[j], c[k])</code>, such that the bitwise <code>XOR</code> of the elements of each triplet has an <strong>even</strong> number of <span data-keyword="set-bit">set bits</span>.
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<div class="example-block">
23+
<p><strong>Input:</strong> <span class="example-io">a = [1], b = [2], c = [3]</span></p>
24+
25+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
26+
27+
<p><strong>Explanation:</strong></p>
28+
29+
<p>The only triplet is <code>(a[0], b[0], c[0])</code> and their <code>XOR</code> is: <code>1 XOR 2 XOR 3 = 00<sub>2</sub></code>.</p>
30+
</div>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">a = [1,1], b = [2,3], c = [1,5]</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">4</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p>Consider these four triplets:</p>
42+
43+
<ul>
44+
<li><code>(a[0], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li>
45+
<li><code>(a[1], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li>
46+
<li><code>(a[0], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li>
47+
<li><code>(a[1], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li>
48+
</ul>
49+
</div>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= a.length, b.length, c.length &lt;= 100</code></li>
56+
<li><code>0 &lt;= a[i], b[i], c[i] &lt;= 100</code></li>
57+
</ul>
58+
59+
<!-- description:end -->
60+
61+
## 解法
62+
63+
<!-- solution:start -->
64+
65+
### 方法一:位运算
66+
67+
对于两个整数,异或结果中 1ドル$ 的个数的奇偶性,取决于两个整数的二进制表示中 1ドル$ 的个数的奇偶性。
68+
69+
我们可以用三个数组 `cnt1``cnt2``cnt3` 分别记录数组 `a``b``c` 中每个数的二进制表示中 1ドル$ 的个数的奇偶性。
70+
71+
然后我们在 $[0, 1]$ 的范围内枚举三个数组中的每个数的二进制表示中 1ドル$ 的个数的奇偶性,如果三个数的二进制表示中 1ドル$ 的个数的奇偶性之和为偶数,那么这三个数的异或结果中 1ドル$ 的个数也为偶数,此时我们将这三个数的组合数相乘累加到答案中。
72+
73+
最后返回答案即可。
74+
75+
时间复杂度 $O(n),ドル其中 $n$ 为数组 `a``b``c` 的长度。空间复杂度 $O(1)$。
76+
77+
<!-- tabs:start -->
78+
79+
#### Python3
80+
81+
```python
82+
class Solution:
83+
def tripletCount(self, a: List[int], b: List[int], c: List[int]) -> int:
84+
cnt1 = Counter(x.bit_count() & 1 for x in a)
85+
cnt2 = Counter(x.bit_count() & 1 for x in b)
86+
cnt3 = Counter(x.bit_count() & 1 for x in c)
87+
ans = 0
88+
for i in range(2):
89+
for j in range(2):
90+
for k in range(2):
91+
if (i + j + k) & 1 ^ 1:
92+
ans += cnt1[i] * cnt2[j] * cnt3[k]
93+
return ans
94+
```
95+
96+
#### Java
97+
98+
```java
99+
class Solution {
100+
public int tripletCount(int[] a, int[] b, int[] c) {
101+
int[] cnt1 = new int[2];
102+
int[] cnt2 = new int[2];
103+
int[] cnt3 = new int[2];
104+
for (int x : a) {
105+
++cnt1[Integer.bitCount(x) & 1];
106+
}
107+
for (int x : b) {
108+
++cnt2[Integer.bitCount(x) & 1];
109+
}
110+
for (int x : c) {
111+
++cnt3[Integer.bitCount(x) & 1];
112+
}
113+
int ans = 0;
114+
for (int i = 0; i < 2; ++i) {
115+
for (int j = 0; j < 2; ++j) {
116+
for (int k = 0; k < 2; ++k) {
117+
if ((i + j + k) % 2 == 0) {
118+
ans += cnt1[i] * cnt2[j] * cnt3[k];
119+
}
120+
}
121+
}
122+
}
123+
return ans;
124+
}
125+
}
126+
```
127+
128+
#### C++
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int tripletCount(vector<int>& a, vector<int>& b, vector<int>& c) {
134+
int cnt1[2]{};
135+
int cnt2[2]{};
136+
int cnt3[2]{};
137+
for (int x : a) {
138+
++cnt1[__builtin_popcount(x) & 1];
139+
}
140+
for (int x : b) {
141+
++cnt2[__builtin_popcount(x) & 1];
142+
}
143+
for (int x : c) {
144+
++cnt3[__builtin_popcount(x) & 1];
145+
}
146+
int ans = 0;
147+
for (int i = 0; i < 2; ++i) {
148+
for (int j = 0; j < 2; ++j) {
149+
for (int k = 0; k < 2; ++k) {
150+
if ((i + j + k) % 2 == 0) {
151+
ans += cnt1[i] * cnt2[j] * cnt3[k];
152+
}
153+
}
154+
}
155+
}
156+
return ans;
157+
}
158+
};
159+
```
160+
161+
#### Go
162+
163+
```go
164+
func tripletCount(a []int, b []int, c []int) (ans int) {
165+
cnt1 := [2]int{}
166+
cnt2 := [2]int{}
167+
cnt3 := [2]int{}
168+
for _, x := range a {
169+
cnt1[bits.OnesCount(uint(x))%2]++
170+
}
171+
for _, x := range b {
172+
cnt2[bits.OnesCount(uint(x))%2]++
173+
}
174+
for _, x := range c {
175+
cnt3[bits.OnesCount(uint(x))%2]++
176+
}
177+
for i := 0; i < 2; i++ {
178+
for j := 0; j < 2; j++ {
179+
for k := 0; k < 2; k++ {
180+
if (i+j+k)%2 == 0 {
181+
ans += cnt1[i] * cnt2[j] * cnt3[k]
182+
}
183+
}
184+
}
185+
}
186+
return
187+
}
188+
```
189+
190+
#### TypeScript
191+
192+
```ts
193+
function tripletCount(a: number[], b: number[], c: number[]): number {
194+
const cnt1: [number, number] = [0, 0];
195+
const cnt2: [number, number] = [0, 0];
196+
const cnt3: [number, number] = [0, 0];
197+
for (const x of a) {
198+
++cnt1[bitCount(x) & 1];
199+
}
200+
for (const x of b) {
201+
++cnt2[bitCount(x) & 1];
202+
}
203+
for (const x of c) {
204+
++cnt3[bitCount(x) & 1];
205+
}
206+
let ans = 0;
207+
for (let i = 0; i < 2; ++i) {
208+
for (let j = 0; j < 2; ++j) {
209+
for (let k = 0; k < 2; ++k) {
210+
if ((i + j + k) % 2 === 0) {
211+
ans += cnt1[i] * cnt2[j] * cnt3[k];
212+
}
213+
}
214+
}
215+
}
216+
return ans;
217+
}
218+
219+
function bitCount(i: number): number {
220+
i = i - ((i >>> 1) & 0x55555555);
221+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
222+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
223+
i = i + (i >>> 8);
224+
i = i + (i >>> 16);
225+
return i & 0x3f;
226+
}
227+
```
228+
229+
<!-- tabs:end -->
230+
231+
<!-- solution:end -->
232+
233+
<!-- problem:end -->

0 commit comments

Comments
(0)

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