Advertisement
Guest User

Untitled

a guest
May 26th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. public class Solution {
  2. public int singleNumber(int[] nums) {
  3. int one = 0;
  4. int two = 0;
  5. int mask = 0;
  6. for(int num : nums){
  7. two ^= num & one;
  8. //two |= num & one;
  9. one ^= num;
  10. mask = ~ (one & two);
  11. one &= mask;
  12. two &= mask;
  13. }
  14.  
  15. return one;
  16. }
  17. }
  18.  
  19. /*
  20.  
  21. 00 => 01 => 10 => 00
  22.  
  23. [7, 7, 7, 3]
  24.  
  25. one 0 0 0
  26. two 0 0 0
  27.  
  28. first 7:
  29. one 1 1 1
  30. two 0 0 0
  31.  
  32. second 7:
  33.  
  34. one 0 0 0
  35. two 1 1 1
  36.  
  37. third 7:
  38.  
  39. one 0 0 0
  40. two 0 0 0
  41.  
  42. first 3:
  43.  
  44. one 0 1 1
  45. two 0 0 0
  46.  
  47. return 3
  48.  
  49.  
  50. extend to five times
  51.  
  52. 000 => 001 => 010 => 011 => 100 => 000
  53. [7, 7, 7, 7, 7, 3]
  54.  
  55. three ^= num & one & two;
  56. two ^= num & one;
  57. one ^= num;
  58.  
  59. mask = ~ (one & ~two & three); //when == 101, go back to 000
  60. one &= mask;
  61. two &= mask;
  62. three &= mask;
  63.  
  64. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement