1679 Max Number of K-Sum Pairs
You are given an integer array nums
and an integer k
.
In one operation, you can pick two numbers from the array whose sum equals k
and remove them from the array.
Return the maximum number of operations you can perform on the array.
Example 1:
Example 2:
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= 109
这题一看就是two sum的变种,可以排序变成167 Two Sum II - input array is sorted。T:(nlogn) S:O(1)。又可以不排序用hashmap变170 Two Sum III - Data structure design,两遍hashmap。看了答案,发现还有一种做法过一遍hashmap的。写一下那个。其实我们只用存现在这个数进hm里。找的时候,看到部署在hm里,就知道这是可以消去的,所以数目减一,count+1,不用再做其他操作了
Last updated