Skip to content

Commit a71b5da

Browse files
committed
feat: update solutions to lc problem: No.2032
No.2032.Two Out of Three
1 parent e476261 commit a71b5da

File tree

7 files changed

+74
-119
lines changed

7 files changed

+74
-119
lines changed

solution/1100-1199/1139.Largest 1-Bordered Square/Solution.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
1111
for k in range(min(m, n), 0, -1):
1212
for i in range(m - k + 1):
1313
for j in range(n - k + 1):
14-
if down[i][j] >= k and right[i][j] >= k and right[i + k - 1][j] >= k and down[i][j + k - 1] >= k:
14+
if (
15+
down[i][j] >= k
16+
and right[i][j] >= k
17+
and right[i + k - 1][j] >= k
18+
and down[i][j + k - 1] >= k
19+
):
1520
return k * k
1621
return 0

solution/1100-1199/1146.Snapshot Array/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class SnapshotArray:
2-
32
def __init__(self, length: int):
43
self.idx = 0
54
self.arr = defaultdict(list)

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

+27-40
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:数组 + 枚举**
56+
57+
我们可以先将每个数组中的元素放入数组中,然后枚举 $1$ 到 $100$ 中的每个数 $i$,判断 $i$ 是否在至少两个数组中出现过。若是,则将 $i$ 加入答案数组中。
58+
59+
时间复杂度 $O(n_1 + n_2 + n_3)$,空间复杂度 $O(n_1 + n_2 + n_3)$。其中 $n_1, n_2, n_3$ 分别为数组 `nums1``nums2``nums3` 的长度。
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
@@ -60,16 +66,9 @@
6066

6167
```python
6268
class Solution:
63-
def twoOutOfThree(
64-
self, nums1: List[int], nums2: List[int], nums3: List[int]
65-
) -> List[int]:
69+
def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
6670
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
67-
ans = []
68-
for i in range(1, 101):
69-
a, b, c = i in s1, i in s2, i in s3
70-
if a + b + c > 1:
71-
ans.append(i)
72-
return ans
71+
return [i for i in range(1, 101) if (i in s1) + (i in s2) + (i in s3) > 1]
7372
```
7473

7574
### **Java**
@@ -105,52 +104,40 @@ class Solution {
105104
class Solution {
106105
public:
107106
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
107+
auto get = [](vector<int>& nums) {
108+
vector<int> cnt(101);
109+
for (int& v :nums) cnt[v] = 1;
110+
return cnt;
111+
};
108112
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
109113
vector<int> ans;
110-
for (int i = 1; i <= 100; ++i)
111-
if (s1[i] + s2[i] + s3[i] > 1)
112-
ans.push_back(i);
114+
for (int i = 1; i <= 100; ++i) {
115+
if (s1[i] + s2[i] + s3[i] > 1) {
116+
ans.emplace_back(i);
117+
}
118+
}
113119
return ans;
114120
}
115-
116-
vector<int> get(vector<int>& nums) {
117-
vector<int> s(101);
118-
for (int num : nums) s[num] = 1;
119-
return s;
120-
}
121121
};
122122
```
123123
124124
### **Go**
125125
126126
```go
127-
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) []int {
127+
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) (ans []int) {
128+
get := func(nums []int) (s [101]int) {
129+
for _, v := range nums {
130+
s[v] = 1
131+
}
132+
return
133+
}
128134
s1, s2, s3 := get(nums1), get(nums2), get(nums3)
129-
var ans []int
130135
for i := 1; i <= 100; i++ {
131-
a, b, c := 0, 0, 0
132-
if s1[i] {
133-
a++
134-
}
135-
if s2[i] {
136-
b++
137-
}
138-
if s3[i] {
139-
c++
140-
}
141-
if a+b+c > 1 {
136+
if s1[i]+s2[i]+s3[i] > 1 {
142137
ans = append(ans, i)
143138
}
144139
}
145-
return ans
146-
}
147-
148-
func get(nums []int) map[int]bool {
149-
s := make(map[int]bool, 101)
150-
for _, num := range nums {
151-
s[num] = true
152-
}
153-
return s
140+
return
154141
}
155142
```
156143

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

+21-40
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,9 @@ Given three integer arrays <code>nums1</code>, <code>nums2</code>, and <code>num
5252

5353
```python
5454
class Solution:
55-
def twoOutOfThree(
56-
self, nums1: List[int], nums2: List[int], nums3: List[int]
57-
) -> List[int]:
55+
def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
5856
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
59-
ans = []
60-
for i in range(1, 101):
61-
a, b, c = i in s1, i in s2, i in s3
62-
if a + b + c > 1:
63-
ans.append(i)
64-
return ans
57+
return [i for i in range(1, 101) if (i in s1) + (i in s2) + (i in s3) > 1]
6558
```
6659

6760
### **Java**
@@ -95,52 +88,40 @@ class Solution {
9588
class Solution {
9689
public:
9790
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
91+
auto get = [](vector<int>& nums) {
92+
vector<int> cnt(101);
93+
for (int& v :nums) cnt[v] = 1;
94+
return cnt;
95+
};
9896
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
9997
vector<int> ans;
100-
for (int i = 1; i <= 100; ++i)
101-
if (s1[i] + s2[i] + s3[i] > 1)
102-
ans.push_back(i);
98+
for (int i = 1; i <= 100; ++i) {
99+
if (s1[i] + s2[i] + s3[i] > 1) {
100+
ans.emplace_back(i);
101+
}
102+
}
103103
return ans;
104104
}
105-
106-
vector<int> get(vector<int>& nums) {
107-
vector<int> s(101);
108-
for (int num : nums) s[num] = 1;
109-
return s;
110-
}
111105
};
112106
```
113107
114108
### **Go**
115109
116110
```go
117-
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) []int {
111+
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) (ans []int) {
112+
get := func(nums []int) (s [101]int) {
113+
for _, v := range nums {
114+
s[v] = 1
115+
}
116+
return
117+
}
118118
s1, s2, s3 := get(nums1), get(nums2), get(nums3)
119-
var ans []int
120119
for i := 1; i <= 100; i++ {
121-
a, b, c := 0, 0, 0
122-
if s1[i] {
123-
a++
124-
}
125-
if s2[i] {
126-
b++
127-
}
128-
if s3[i] {
129-
c++
130-
}
131-
if a+b+c > 1 {
120+
if s1[i]+s2[i]+s3[i] > 1 {
132121
ans = append(ans, i)
133122
}
134123
}
135-
return ans
136-
}
137-
138-
func get(nums []int) map[int]bool {
139-
s := make(map[int]bool, 101)
140-
for _, num := range nums {
141-
s[num] = true
142-
}
143-
return s
124+
return
144125
}
145126
```
146127

Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
class Solution {
22
public:
33
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
4+
auto get = [](vector<int>& nums) {
5+
vector<int> cnt(101);
6+
for (int& v :nums) cnt[v] = 1;
7+
return cnt;
8+
};
49
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
510
vector<int> ans;
6-
for (int i = 1; i <= 100; ++i)
7-
if (s1[i] + s2[i] + s3[i] > 1)
8-
ans.push_back(i);
11+
for (int i = 1; i <= 100; ++i) {
12+
if (s1[i] + s2[i] + s3[i] > 1) {
13+
ans.emplace_back(i);
14+
}
15+
}
916
return ans;
1017
}
11-
12-
vector<int> get(vector<int>& nums) {
13-
vector<int> s(101);
14-
for (int num : nums) s[num] = 1;
15-
return s;
16-
}
1718
};
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
1-
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) []int {
1+
func twoOutOfThree(nums1 []int, nums2 []int, nums3 []int) (ans []int) {
2+
get := func(nums []int) (s [101]int) {
3+
for _, v := range nums {
4+
s[v] = 1
5+
}
6+
return
7+
}
28
s1, s2, s3 := get(nums1), get(nums2), get(nums3)
3-
var ans []int
49
for i := 1; i <= 100; i++ {
5-
a, b, c := 0, 0, 0
6-
if s1[i] {
7-
a++
8-
}
9-
if s2[i] {
10-
b++
11-
}
12-
if s3[i] {
13-
c++
14-
}
15-
if a+b+c > 1 {
10+
if s1[i]+s2[i]+s3[i] > 1 {
1611
ans = append(ans, i)
1712
}
1813
}
19-
return ans
20-
}
21-
22-
func get(nums []int) map[int]bool {
23-
s := make(map[int]bool, 101)
24-
for _, num := range nums {
25-
s[num] = true
26-
}
27-
return s
14+
return
2815
}

solution/2000-2099/2032.Two Out of Three/Solution.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,4 @@ def twoOutOfThree(
33
self, nums1: List[int], nums2: List[int], nums3: List[int]
44
) -> List[int]:
55
s1, s2, s3 = set(nums1), set(nums2), set(nums3)
6-
ans = []
7-
for i in range(1, 101):
8-
a, b, c = i in s1, i in s2, i in s3
9-
if a + b + c > 1:
10-
ans.append(i)
11-
return ans
6+
return [i for i in range(1, 101) if (i in s1) + (i in s2) + (i in s3) > 1]

0 commit comments

Comments
 (0)