Skip to content

Instantly share code, notes, and snippets.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
/*
x ^ 0 = x;
x ^ x = 0;
x ^ x ^ x = x;
*/
public class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int num : nums){
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 );
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
while( m < n){
n &= (n-1);
}
return n;
}
}
class Solution {
public int singleNumber(int[] nums) {
int x1 = 0, x2 = 0, mask = 0;
for (int n : nums) {
x2 = x2 ^ (x1 & n);
x1 = x1 ^ n;
mask = ~ (x2 & x1);
x1 = x1 & mask;
/*
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
*/
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
if (n == 0) return 0;
int res = 0;
for(int i = 0; i < 32; i++){
res <<= 1;