题解 | #只出现一次的牛II#
只出现一次的牛II
https://www.nowcoder.com/practice/fde24d7d8f97467e91403d255243ee1c
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector<int> findSingleCowsII(vector<int>& nums) { // write code here // 分治+二进制 int temp = 0; for(auto &num : nums){ temp ^= num; } int s = 0 ; while(temp){ if(temp & 1) break; s++; temp >>=1; } int x = 0, y = 0; for(auto &num : nums){ if((num>>s) & 1) x ^= num; else y ^= num; } if(x < y) return {x, y}; else return {y, x}; } };