Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public int singleNumber(int[] nums) {
- int one = 0;
- int two = 0;
- int mask = 0;
- for(int num : nums){
- two ^= num & one;
- //two |= num & one;
- one ^= num;
- mask = ~ (one & two);
- one &= mask;
- two &= mask;
- }
- return one;
- }
- }
- /*
- 00 => 01 => 10 => 00
- [7, 7, 7, 3]
- one 0 0 0
- two 0 0 0
- first 7:
- one 1 1 1
- two 0 0 0
- second 7:
- one 0 0 0
- two 1 1 1
- third 7:
- one 0 0 0
- two 0 0 0
- first 3:
- one 0 1 1
- two 0 0 0
- return 3
- extend to five times
- 000 => 001 => 010 => 011 => 100 => 000
- [7, 7, 7, 7, 7, 3]
- three ^= num & one & two;
- two ^= num & one;
- one ^= num;
- mask = ~ (one & ~two & three); //when == 101, go back to 000
- one &= mask;
- two &= mask;
- three &= mask;
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement