Given An array of numbers nums
, in which exactly-elements appear only once and all the other elements appear exactly Twice. Find the elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5]
, return[3, 5]
classSolution { Public: Vector<int> Singlenumber (vector<int>&nums) { intX_xor_y =0; for(intn:nums) {x_xor_y^=N; } //find the last one 1//1 indicates that the value of this point two is different, one is 1 is 1, otherwise it cannot be different or appear 1 intMask = x_xor_y & ~ (X_xor_y-1); intx =0; inty =0; //X:xor of A,a,b,b,x//Y:xor of C,c,y for(intn:nums) { if(N &mask) {x^=N; } Else{y^=N; } } returnvector<int>{x, y};}};
Given an array of integers, every element appears twice except for one. Find the single one.
class Solution {public: int singlenumber (intint N) { int0; for 0; I < n; + +i) {^= a[i] ; } return num; }};
Given an array of integers, every element appears three times except for one. Find the single one.
int singlenumber (vector<int> AA) { int00; // int size = sizeof (AA)/sizeof (int); for (int0; i < aa.size (); i++) { = (ones ^ aa[i]) & ~twos; = (twos ^ aa[i]) & ~ones; } return ones;}
Only one occurrence of the number