Skip to content

Commit 7477cb8

Browse files
committed
feat: add solutions to lc problem: No.2032
No.2032.Two Out of Three
1 parent 30fef90 commit 7477cb8

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

solution/2000-2099/2032.Two Out of Three/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,61 @@ func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) (ans []int) {
141141
}
142142
```
143143

144+
### **TypeScript**
145+
146+
```ts
147+
function twoOutOfThree(
148+
nums1: number[],
149+
nums2: number[],
150+
nums3: number[],
151+
): number[] {
152+
const count = new Array(101).fill(0);
153+
new Set(nums1).forEach(v => count[v]++);
154+
new Set(nums2).forEach(v => count[v]++);
155+
new Set(nums3).forEach(v => count[v]++);
156+
const ans = [];
157+
count.forEach((v, i) => {
158+
if (v >= 2) {
159+
ans.push(i);
160+
}
161+
});
162+
return ans;
163+
}
164+
```
165+
166+
### **Rust**
167+
168+
```rust
169+
use std::collections::HashSet;
170+
impl Solution {
171+
pub fn two_out_of_three(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>) -> Vec<i32> {
172+
let mut count = vec![0; 101];
173+
nums1
174+
.into_iter()
175+
.collect::<HashSet<i32>>()
176+
.iter()
177+
.for_each(|&v| count[v as usize] += 1);
178+
nums2
179+
.into_iter()
180+
.collect::<HashSet<i32>>()
181+
.iter()
182+
.for_each(|&v| count[v as usize] += 1);
183+
nums3
184+
.into_iter()
185+
.collect::<HashSet<i32>>()
186+
.iter()
187+
.for_each(|&v| count[v as usize] += 1);
188+
let mut ans = Vec::new();
189+
count.iter().enumerate().for_each(|(i, v)| {
190+
if *v >= 2 {
191+
ans.push(i as i32);
192+
}
193+
});
194+
ans
195+
}
196+
}
197+
```
198+
144199
### **...**
145200

146201
```

solution/2000-2099/2032.Two Out of Three/README_EN.md

+55
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,61 @@ func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) (ans []int) {
125125
}
126126
```
127127

128+
### **TypeScript**
129+
130+
```ts
131+
function twoOutOfThree(
132+
nums1: number[],
133+
nums2: number[],
134+
nums3: number[],
135+
): number[] {
136+
const count = new Array(101).fill(0);
137+
new Set(nums1).forEach(v => count[v]++);
138+
new Set(nums2).forEach(v => count[v]++);
139+
new Set(nums3).forEach(v => count[v]++);
140+
const ans = [];
141+
count.forEach((v, i) => {
142+
if (v >= 2) {
143+
ans.push(i);
144+
}
145+
});
146+
return ans;
147+
}
148+
```
149+
150+
### **Rust**
151+
152+
```rust
153+
use std::collections::HashSet;
154+
impl Solution {
155+
pub fn two_out_of_three(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>) -> Vec<i32> {
156+
let mut count = vec![0; 101];
157+
nums1
158+
.into_iter()
159+
.collect::<HashSet<i32>>()
160+
.iter()
161+
.for_each(|&v| count[v as usize] += 1);
162+
nums2
163+
.into_iter()
164+
.collect::<HashSet<i32>>()
165+
.iter()
166+
.for_each(|&v| count[v as usize] += 1);
167+
nums3
168+
.into_iter()
169+
.collect::<HashSet<i32>>()
170+
.iter()
171+
.for_each(|&v| count[v as usize] += 1);
172+
let mut ans = Vec::new();
173+
count.iter().enumerate().for_each(|(i, v)| {
174+
if *v >= 2 {
175+
ans.push(i as i32);
176+
}
177+
});
178+
ans
179+
}
180+
}
181+
```
182+
128183
### **...**
129184

130185
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn two_out_of_three(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>) -> Vec<i32> {
4+
let mut count = vec![0; 101];
5+
nums1
6+
.into_iter()
7+
.collect::<HashSet<i32>>()
8+
.iter()
9+
.for_each(|&v| count[v as usize] += 1);
10+
nums2
11+
.into_iter()
12+
.collect::<HashSet<i32>>()
13+
.iter()
14+
.for_each(|&v| count[v as usize] += 1);
15+
nums3
16+
.into_iter()
17+
.collect::<HashSet<i32>>()
18+
.iter()
19+
.for_each(|&v| count[v as usize] += 1);
20+
let mut ans = Vec::new();
21+
count.iter().enumerate().for_each(|(i, v)| {
22+
if *v >= 2 {
23+
ans.push(i as i32);
24+
}
25+
});
26+
ans
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function twoOutOfThree(
2+
nums1: number[],
3+
nums2: number[],
4+
nums3: number[],
5+
): number[] {
6+
const count = new Array(101).fill(0);
7+
new Set(nums1).forEach(v => count[v]++);
8+
new Set(nums2).forEach(v => count[v]++);
9+
new Set(nums3).forEach(v => count[v]++);
10+
const ans = [];
11+
count.forEach((v, i) => {
12+
if (v >= 2) {
13+
ans.push(i);
14+
}
15+
});
16+
return ans;
17+
}

0 commit comments

Comments
 (0)