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]
.
Note:
- The order of the result is not important. The above example, is
[5, 3]
also correct.
- Your algorithm should run in linear runtime complexity. Could implement it using only constant space complexity?
Runtime:16ms
1 classSolution {2 Public:3vector<int> Singlenumber (vector<int>&nums) {4vector<int>result;5 6 inttemp =0;7 for(inti =0; I < nums.size (); i++)8Temp ^=Nums[i];9 Ten intMark =1; One while(Temp & mark)! =Mark) AMark <<=1; - - intx =0; the for(inti =0; I < nums.size (); i++){ - if(Nums[i] &Mark) -X ^=Nums[i]; - } + - inty = temp ^x; + A result.push_back (x); at result.push_back (y); - returnresult; - } -};
Single number III