2341. Maximum Number of Pairs in Array, My C++ code is Accepted. 中文解說

Leetcode Weekly Contest 302
2341. Maximum Number of Pairs in Array, My C++ code is Accepted.
中文解說

You are given a 0-indexed integer array nums. In one operation, you may do the following:

Choose two integers in nums that are equal.
Remove both integers from nums, forming a pair.
The operation is done on nums as many times as possible.

Return a 0-indexed integer array answer of size 2 where answer[0] is the number of pairs that are formed and answer[1] is the number of leftover integers in nums after doing the operation as many times as possible.

class Solution {
public:
vector numberOfPairs(vector& nums) {
vector vReturn;
vector vCl = nums;
int iCount = 0;
for(int i =0; i < vCl.size(); i++) {
for(int j = i+1 ; j < vCl.size(); j++){
if(vCl[i] == vCl[j]){

iCount++;
vCl.erase(vCl.begin()+j);
break;
}
}

}
vReturn.push_back(iCount);
vReturn.push_back(nums.size() - (iCount*2));
return vReturn;
}
};

以上。
祝你順心。

Billour Ou
歐育溙

2022/8/2

參考

Leetcode

HackerRank