使数组异或等于 K 的最少运算次数
题目 使数组异或等于 K 的最少运算次数
思路分析
代码实现
class Solution {
public:
int lowbit(int x){
return x&-x;
}
int minOperations(vector<int>& nums, int k) {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int target=k,cnt=0;
for(auto x:nums){
target^=x;
}
while(target){
target-=lowbit(target);
cnt++;
}
return cnt;
}
};
💬 评论