61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
- ** 方法一:排序 + 哈希表 **
64
+ ** 方法一:排序**
65
65
66
- 我们先对数组进行排序,然后遍历数组,将 $nums[ i ] $ 与 $nums [ n-i-1 ] $ 求和后放入哈希表中,最后返回哈希表的大小即可 。
66
+ 题目中要求每次找到数组 $nums$ 中的最小值和最大值,然后删除它们,再计算删除两数的平均值。因此,我们可以先对数组 $nums$ 进行排序,然后每次取数组的首尾元素,计算它们的和,用哈希表或数组 $cnt$ 记录每个和出现的次数,最后统计不同的和的个数即可 。
67
67
68
- 时间复杂度 $O(n\times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度 。
68
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度 。
69
69
70
70
<!-- tabs:start -->
71
71
76
76
``` python
77
77
class Solution :
78
78
def distinctAverages (self , nums : List[int ]) -> int :
79
- n = len (nums)
80
79
nums.sort()
81
- return len (set (nums[i] + nums[n - i - 1 ] for i in range (n >> 1 )))
80
+ return len (set (nums[i] + nums[- i - 1 ] for i in range (len (nums) >> 1 )))
81
+ ```
82
+
83
+ ``` python
84
+ class Solution :
85
+ def distinctAverages (self , nums : List[int ]) -> int :
86
+ nums.sort()
87
+ ans = 0
88
+ cnt = Counter()
89
+ for i in range (len (nums) >> 1 ):
90
+ x = nums[i] + nums[- i - 1 ]
91
+ cnt[x] += 1
92
+ if cnt[x] == 1 :
93
+ ans += 1
94
+ return ans
82
95
```
83
96
84
97
### ** Java**
@@ -89,35 +102,72 @@ class Solution:
89
102
class Solution {
90
103
public int distinctAverages (int [] nums ) {
91
104
Arrays . sort(nums);
92
- int n = nums. length;
93
105
Set<Integer > s = new HashSet<> ();
94
- for (int i = 0 ; i< n> > 1 ; ++ i) {
106
+ int n = nums. length;
107
+ for (int i = 0 ; i < n >> 1 ; ++ i) {
95
108
s. add(nums[i] + nums[n - i - 1 ]);
96
109
}
97
110
return s. size();
98
111
}
99
112
}
100
113
```
101
114
115
+ ``` java
116
+ class Solution {
117
+ public int distinctAverages (int [] nums ) {
118
+ Arrays . sort(nums);
119
+ int [] cnt = new int [201 ];
120
+ int n = nums. length;
121
+ int ans = 0 ;
122
+ for (int i = 0 ; i < n >> 1 ; ++ i) {
123
+ if (++ cnt[nums[i] + nums[n - i - 1 ]] == 1 ) {
124
+ ++ ans;
125
+ }
126
+ }
127
+ return ans;
128
+ }
129
+ }
130
+ ```
131
+
102
132
### ** C++**
103
133
104
134
``` cpp
105
135
class Solution {
106
136
public:
107
137
int distinctAverages(vector<int >& nums) {
108
138
sort(nums.begin(), nums.end());
109
- int n = nums.size();
110
139
unordered_set<int > s;
111
- for (int i = 0; i < n >> 1; ++i) s.insert(nums[ i] + nums[ n - i - 1] );
140
+ int n = nums.size();
141
+ for (int i = 0; i < n >> 1; ++i) {
142
+ s.insert(nums[ i] + nums[ n - i - 1] );
143
+ }
112
144
return s.size();
113
145
}
114
146
};
115
147
```
116
148
149
+ ```cpp
150
+ class Solution {
151
+ public:
152
+ int distinctAverages(vector<int>& nums) {
153
+ sort(nums.begin(), nums.end());
154
+ int cnt[201]{};
155
+ int n = nums.size();
156
+ int ans = 0;
157
+ for (int i = 0; i < n >> 1; ++i) {
158
+ if (++cnt[nums[i] + nums[n - i - 1]] == 1) {
159
+ ++ans;
160
+ }
161
+ }
162
+ return ans;
163
+ }
164
+ };
165
+ ```
166
+
117
167
### ** Go**
118
168
119
169
``` go
120
- func distinctAverages(nums []int) int {
170
+ func distinctAverages (nums []int ) ( ans int ) {
121
171
sort.Ints (nums)
122
172
n := len (nums)
123
173
s := map [int ]struct {}{}
@@ -128,10 +178,49 @@ func distinctAverages(nums []int) int {
128
178
}
129
179
```
130
180
181
+ ``` go
182
+ func distinctAverages (nums []int ) (ans int ) {
183
+ sort.Ints (nums)
184
+ n := len (nums)
185
+ cnt := [201 ]int {}
186
+ for i := 0 ; i < n>>1 ; i++ {
187
+ x := nums[i] + nums[n-i-1 ]
188
+ cnt[x]++
189
+ if cnt[x] == 1 {
190
+ ans++
191
+ }
192
+ }
193
+ return
194
+ }
195
+ ```
196
+
131
197
### ** TypeScript**
132
198
133
199
``` ts
200
+ function distinctAverages(nums : number []): number {
201
+ nums .sort ((a , b ) => a - b );
202
+ const s: Set <number > = new Set ();
203
+ const n = nums .length ;
204
+ for (let i = 0 ; i < n >> 1 ; ++ i ) {
205
+ s .add (nums [i ] + nums [n - i - 1 ]);
206
+ }
207
+ return s .size ;
208
+ }
209
+ ```
134
210
211
+ ``` ts
212
+ function distinctAverages(nums : number []): number {
213
+ nums .sort ((a , b ) => a - b );
214
+ const cnt: number [] = Array (201 ).fill (0 );
215
+ let ans = 0 ;
216
+ const n = nums .length ;
217
+ for (let i = 0 ; i < n >> 1 ; ++ i ) {
218
+ if (++ cnt [nums [i ] + nums [n - i - 1 ]] === 1 ) {
219
+ ++ ans ;
220
+ }
221
+ }
222
+ return ans ;
223
+ }
135
224
```
136
225
137
226
### ** ...**
0 commit comments