2206. Divide Array Into Equal Pairs - Easy
前往題目
思路
這題也十分簡單,只要看是否每個數字都有對應的自己就好
Code
class Solution {
public boolean divideArray(int[] nums) {
// Base case
if (nums.length < 2) return false;
Set<Integer> appear = new HashSet();
int res = 0;
for (int num : nums) {
if (appear.contains(num)) {
res -= num;
appear.remove(num);
} else {
res += num;
appear.add(num);
}
}
return res == 0;
}
}
2206. Divide Array Into Equal Pairs - Easy
https://f88083.github.io/2025/03/17/2206-Divide-Array-Into-Equal-Pairs-Easy/