49
49
50
50
** 方法一:排序**
51
51
52
- 对 nums 进行排序。然后 l, r 分别指向 nums 首尾元素,判断两整数之和 s 与 k 的大小关系。
52
+ 我们对 ` nums ` 进行排序。然后 $l$, $r$ 分别指向 ` nums ` 首尾元素,判断两整数之和 $s$ 与 $k$ 的大小关系。
53
53
54
- - 若 ` s == k ` ,说明找到了两个整数,满足和为 k,答案 ans 加 1 ,然后 l, r 向中间移动;
55
- - 若 ` s > k ` ,则 r 指针向左移动;
56
- - 若 ` s > k ` ,则 l 指针向右移动;
57
- - 继续循环判断,直至 l >= r 。
54
+ - 若 $ s = k$ ,说明找到了两个整数,满足和为 $k$,答案加一 ,然后 $l$, $r$ 向中间移动;
55
+ - 若 $s \gt k$ ,则 $r$ 指针向左移动;
56
+ - 若 $s \lt k$ ,则 $l$ 指针向右移动;
57
+ - 继续循环判断,直至 $l \geq r$ 。
58
58
59
- 循环结束,返回 ans 。
59
+ 循环结束,返回答案 。
60
60
61
- 时间复杂度 $O(n\times \log n)$,其中 $n$ 为 ` nums ` 的长度。
61
+ 时间复杂度 $O(n\times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为 ` nums ` 的长度。
62
+
63
+ ** 方法二:哈希表**
64
+
65
+ 我们使用哈希表 ` cnt ` 记录当前剩余整数及其出现的次数。
66
+
67
+ 遍历 ` nums ` ,对于当前整数 $x$,判断 $k - x$ 是否在 ` cnt ` 中,若存在,则说明找到了两个整数,满足和为 $k$,答案加一,然后将 $k - x$ 的出现次数减一;否则,将 $x$ 的出现次数加一。
68
+
69
+ 遍历结束,返回答案。
70
+
71
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 ` nums ` 的长度。
62
72
63
73
<!-- tabs:start -->
64
74
@@ -83,6 +93,20 @@ class Solution:
83
93
return ans
84
94
```
85
95
96
+ ``` python
97
+ class Solution :
98
+ def maxOperations (self , nums : List[int ], k : int ) -> int :
99
+ cnt = Counter()
100
+ ans = 0
101
+ for x in nums:
102
+ if cnt[k - x]:
103
+ ans += 1
104
+ cnt[k - x] -= 1
105
+ else :
106
+ cnt[x] += 1
107
+ return ans
108
+ ```
109
+
86
110
### ** Java**
87
111
88
112
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -110,6 +134,26 @@ class Solution {
110
134
}
111
135
```
112
136
137
+ ``` java
138
+ class Solution {
139
+ public int maxOperations (int [] nums , int k ) {
140
+ Map<Integer , Integer > cnt = new HashMap<> ();
141
+ int ans = 0 ;
142
+ for (int x : nums) {
143
+ if (cnt. containsKey(k - x)) {
144
+ ++ ans;
145
+ if (cnt. merge(k - x, - 1 , Integer :: sum) == 0 ) {
146
+ cnt. remove(k - x);
147
+ }
148
+ } else {
149
+ cnt. merge(x, 1 , Integer :: sum);
150
+ }
151
+ }
152
+ return ans;
153
+ }
154
+ }
155
+ ```
156
+
113
157
### ** C++**
114
158
115
159
``` cpp
@@ -135,6 +179,25 @@ public:
135
179
};
136
180
```
137
181
182
+ ```cpp
183
+ class Solution {
184
+ public:
185
+ int maxOperations(vector<int>& nums, int k) {
186
+ unordered_map<int, int> cnt;
187
+ int ans = 0;
188
+ for (int& x : nums) {
189
+ if (cnt[k - x]) {
190
+ --cnt[k - x];
191
+ ++ans;
192
+ } else {
193
+ ++cnt[x];
194
+ }
195
+ }
196
+ return ans;
197
+ }
198
+ };
199
+ ```
200
+
138
201
### ** Go**
139
202
140
203
``` go
@@ -157,6 +220,39 @@ func maxOperations(nums []int, k int) int {
157
220
}
158
221
```
159
222
223
+ ``` go
224
+ func maxOperations (nums []int , k int ) (ans int ) {
225
+ cnt := map [int ]int {}
226
+ for _ , x := range nums {
227
+ if cnt[k-x] > 0 {
228
+ cnt[k-x]--
229
+ ans++
230
+ } else {
231
+ cnt[x]++
232
+ }
233
+ }
234
+ return
235
+ }
236
+ ```
237
+
238
+ ### ** TypeScript**
239
+
240
+ ``` ts
241
+ function maxOperations(nums : number [], k : number ): number {
242
+ const cnt = new Map ();
243
+ let ans = 0 ;
244
+ for (const x of nums ) {
245
+ if (cnt .get (k - x )) {
246
+ cnt .set (k - x , cnt .get (k - x ) - 1 );
247
+ ++ ans ;
248
+ } else {
249
+ cnt .set (x , (cnt .get (x ) | 0 ) + 1 );
250
+ }
251
+ }
252
+ return ans ;
253
+ }
254
+ ```
255
+
160
256
### ** ...**
161
257
162
258
```
0 commit comments