Skip to content

Commit fbd2ce8

Browse files
committed
feat: add solutions to lc problem: No.0982
No.0982.Triples with Bitwise AND Equal To Zero
1 parent e08f8dc commit fbd2ce8

File tree

7 files changed

+256
-5
lines changed

7 files changed

+256
-5
lines changed

solution/0900-0999/0982.Triples with Bitwise AND Equal To Zero/README.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,116 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:枚举 + 计数**
62+
63+
我们可以先枚举任意两个数 $x$ 和 $y$,用哈希表或数组 $cnt$ 统计它们的按位与结果 $x \& y$ 出现的次数。
64+
65+
然后我们枚举 $x$ 和 $y$ 的按位与结果 $xy$,再枚举 $z$,如果 $xy \& z = 0$,则将 $cnt[xy]$ 的值加入答案。
66+
67+
最后返回答案即可。
68+
69+
时间复杂度 $O(n^2 + n \times M)$,空间复杂度 $O(M)$,其中 $n$ 是数组 $nums$ 的长度;而 $M$ 是数组 $nums$ 中的最大值,本题中 $M \leq 2^{16}$。
70+
6171
<!-- tabs:start -->
6272

6373
### **Python3**
6474

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

6777
```python
68-
78+
class Solution:
79+
def countTriplets(self, nums: List[int]) -> int:
80+
cnt = Counter(x & y for x in nums for y in nums)
81+
return sum(v for xy, v in cnt.items() for z in nums if xy & z == 0)
6982
```
7083

7184
### **Java**
7285

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

7588
```java
89+
class Solution {
90+
public int countTriplets(int[] nums) {
91+
int mx = 0;
92+
for (int x : nums) {
93+
mx = Math.max(mx, x);
94+
}
95+
int[] cnt = new int[mx + 1];
96+
for (int x : nums) {
97+
for (int y : nums) {
98+
cnt[x & y]++;
99+
}
100+
}
101+
int ans = 0;
102+
for (int xy = 0; xy <= mx; ++xy) {
103+
for (int z : nums) {
104+
if ((xy & z) == 0) {
105+
ans += cnt[xy];
106+
}
107+
}
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int countTriplets(vector<int>& nums) {
120+
int mx = *max_element(nums.begin(), nums.end());
121+
int cnt[mx + 1];
122+
memset(cnt, 0, sizeof cnt);
123+
for (int& x : nums) {
124+
for (int& y : nums) {
125+
cnt[x & y]++;
126+
}
127+
}
128+
int ans = 0;
129+
for (int xy = 0; xy <= mx; ++xy) {
130+
for (int& z : nums) {
131+
if ((xy & z) == 0) {
132+
ans += cnt[xy];
133+
}
134+
}
135+
}
136+
return ans;
137+
}
138+
};
139+
```
76140
141+
### **Go**
142+
143+
```go
144+
func countTriplets(nums []int) (ans int) {
145+
mx := 0
146+
for _, x := range nums {
147+
mx = max(mx, x)
148+
}
149+
cnt := make([]int, mx+1)
150+
for _, x := range nums {
151+
for _, y := range nums {
152+
cnt[x&y]++
153+
}
154+
}
155+
for xy := 0; xy <= mx; xy++ {
156+
for _, z := range nums {
157+
if xy&z == 0 {
158+
ans += cnt[xy]
159+
}
160+
}
161+
}
162+
return
163+
}
164+
165+
func max(a, b int) int {
166+
if a > b {
167+
return a
168+
}
169+
return b
170+
}
77171
```
78172

79173
### **...**

solution/0900-0999/0982.Triples with Bitwise AND Equal To Zero/README_EN.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,97 @@
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def countTriplets(self, nums: List[int]) -> int:
63+
cnt = Counter(x & y for x in nums for y in nums)
64+
return sum(v for xy, v in cnt.items() for z in nums if xy & z == 0)
6265
```
6366

6467
### **Java**
6568

6669
```java
70+
class Solution {
71+
public int countTriplets(int[] nums) {
72+
int mx = 0;
73+
for (int x : nums) {
74+
mx = Math.max(mx, x);
75+
}
76+
int[] cnt = new int[mx + 1];
77+
for (int x : nums) {
78+
for (int y : nums) {
79+
cnt[x & y]++;
80+
}
81+
}
82+
int ans = 0;
83+
for (int xy = 0; xy <= mx; ++xy) {
84+
for (int z : nums) {
85+
if ((xy & z) == 0) {
86+
ans += cnt[xy];
87+
}
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int countTriplets(vector<int>& nums) {
101+
int mx = *max_element(nums.begin(), nums.end());
102+
int cnt[mx + 1];
103+
memset(cnt, 0, sizeof cnt);
104+
for (int& x : nums) {
105+
for (int& y : nums) {
106+
cnt[x & y]++;
107+
}
108+
}
109+
int ans = 0;
110+
for (int xy = 0; xy <= mx; ++xy) {
111+
for (int& z : nums) {
112+
if ((xy & z) == 0) {
113+
ans += cnt[xy];
114+
}
115+
}
116+
}
117+
return ans;
118+
}
119+
};
120+
```
67121
122+
### **Go**
123+
124+
```go
125+
func countTriplets(nums []int) (ans int) {
126+
mx := 0
127+
for _, x := range nums {
128+
mx = max(mx, x)
129+
}
130+
cnt := make([]int, mx+1)
131+
for _, x := range nums {
132+
for _, y := range nums {
133+
cnt[x&y]++
134+
}
135+
}
136+
for xy := 0; xy <= mx; xy++ {
137+
for _, z := range nums {
138+
if xy&z == 0 {
139+
ans += cnt[xy]
140+
}
141+
}
142+
}
143+
return
144+
}
145+
146+
func max(a, b int) int {
147+
if a > b {
148+
return a
149+
}
150+
return b
151+
}
68152
```
69153

70154
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int countTriplets(vector<int>& nums) {
4+
int mx = *max_element(nums.begin(), nums.end());
5+
int cnt[mx + 1];
6+
memset(cnt, 0, sizeof cnt);
7+
for (int& x : nums) {
8+
for (int& y : nums) {
9+
cnt[x & y]++;
10+
}
11+
}
12+
int ans = 0;
13+
for (int xy = 0; xy <= mx; ++xy) {
14+
for (int& z : nums) {
15+
if ((xy & z) == 0) {
16+
ans += cnt[xy];
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func countTriplets(nums []int) (ans int) {
2+
mx := 0
3+
for _, x := range nums {
4+
mx = max(mx, x)
5+
}
6+
cnt := make([]int, mx+1)
7+
for _, x := range nums {
8+
for _, y := range nums {
9+
cnt[x&y]++
10+
}
11+
}
12+
for xy := 0; xy <= mx; xy++ {
13+
for _, z := range nums {
14+
if xy&z == 0 {
15+
ans += cnt[xy]
16+
}
17+
}
18+
}
19+
return
20+
}
21+
22+
func max(a, b int) int {
23+
if a > b {
24+
return a
25+
}
26+
return b
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int countTriplets(int[] nums) {
3+
int mx = 0;
4+
for (int x : nums) {
5+
mx = Math.max(mx, x);
6+
}
7+
int[] cnt = new int[mx + 1];
8+
for (int x : nums) {
9+
for (int y : nums) {
10+
cnt[x & y]++;
11+
}
12+
}
13+
int ans = 0;
14+
for (int xy = 0; xy <= mx; ++xy) {
15+
for (int z : nums) {
16+
if ((xy & z) == 0) {
17+
ans += cnt[xy];
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def countTriplets(self, nums: List[int]) -> int:
3+
cnt = Counter(x & y for x in nums for y in nums)
4+
return sum(v for xy, v in cnt.items() for z in nums if xy & z == 0)

solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@
5757
<p><strong>Constraints:</strong></p>
5858

5959
<ul>
60-
6160
<li>The number of nodes in the list is in the range&nbsp;<code>[1, 10<sup>5</sup>]</code></li>
62-
6361
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
64-
6562
</ul>
6663

6764
## Solutions

0 commit comments

Comments
 (0)