剑指offer位运算56 - I. 数组中数字出现的次数
Posted trevo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指offer位运算56 - I. 数组中数字出现的次数相关的知识,希望对你有一定的参考价值。
题目链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/
位运算
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
int s = 0;
for(int num : nums)
s ^= num;
int k = s & (-s);
vector<int> rs(2, 0);
for(int num : nums)
{
if(num & k) rs[0] ^= num;
else rs[1] ^= num;
}
return rs;
}
};
以上是关于剑指offer位运算56 - I. 数组中数字出现的次数的主要内容,如果未能解决你的问题,请参考以下文章