|
52 | 52 |
|
53 | 53 | <!-- 这里可写通用的实现逻辑 -->
|
54 | 54 |
|
| 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 | + |
55 | 61 | <!-- tabs:start -->
|
56 | 62 |
|
57 | 63 | ### **Python3**
|
|
60 | 66 |
|
61 | 67 | ```python
|
62 | 68 | 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]: |
66 | 70 | 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] |
73 | 72 | ```
|
74 | 73 |
|
75 | 74 | ### **Java**
|
@@ -105,52 +104,40 @@ class Solution {
|
105 | 104 | class Solution {
|
106 | 105 | public:
|
107 | 106 | 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 | + }; |
108 | 112 | auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
|
109 | 113 | 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 | + } |
113 | 119 | return ans;
|
114 | 120 | }
|
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 |
| - } |
121 | 121 | };
|
122 | 122 | ```
|
123 | 123 |
|
124 | 124 | ### **Go**
|
125 | 125 |
|
126 | 126 | ```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 | + } |
128 | 134 | s1, s2, s3 := get(nums1), get(nums2), get(nums3)
|
129 |
| - var ans []int |
130 | 135 | 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 { |
142 | 137 | ans = append(ans, i)
|
143 | 138 | }
|
144 | 139 | }
|
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 |
154 | 141 | }
|
155 | 142 | ```
|
156 | 143 |
|
|
0 commit comments