Created
May 25, 2017 18:36
-
-
Save BiruLyu/643ced5fa530b5f5fb7dabfbdc615026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int[] singleNumber(int[] nums) { | |
| // Get the XOR of the two numbers we need to find | |
| int xortwo = 0; | |
| for(int num : nums){ | |
| xortwo ^= num; | |
| } | |
| // Get its last set bit | |
| int mask = xortwo & ( -xortwo ); | |
| int[] res = {0, 0}; | |
| for(int num : nums){ | |
| if((num & mask) == 0){ | |
| res[0] ^= num; | |
| } else { | |
| res[1] ^= num; | |
| } | |
| } | |
| return res; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment