LeetCode 3020. 子集中元素的最大数量
原题链接
中等
作者:
我是java同学
,
2024-01-29 10:41:40
,
所有人可见
,
阅读 37
class Solution {
public:
typedef long long LL;
int maximumLength(vector<int>& nums) {
unordered_map<LL, int> cnt;
for (auto x: nums) cnt[x] ++ ;
int n = nums.size();
int res = cnt[1] - (cnt[1] % 2 ^ 1);
cnt[1] = 0;
for (int i = 0; i < n; i ++ ) {
int s = 0;
long long x = nums[i];
while (1) {
if (cnt[x] == 0) {
s -= 1;
break;
}
if (cnt[x] == 1) {
s += 1;
break;
}
s += 2;
x *= x;
}
res = max(res, s);
}
return res;
}
};