Single number III
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?
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.
x &=-X is used in a tree-like array of lowbit.
1 classSolution {2 Public:3vector<int> Singlenumber (vector<int>&nums) {4 intx =0;5 for(auto n:nums) x ^=N;6 intTMP = x, idx =0;7X &=-x;8 intA =0, B =0;9 for(Auto n:nums) {Ten if(N & X) a ^=N; One Elseb ^=N; A } - return{min (A, b), Max (A, B)}; - } the};
[Leetcode] Single number III