Given an array of integers, every element appears twice except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Title Analysis: Each number of digits in the array, each digit acceptance appears, only one number appears once, we know that the XOR operator is the same as 0 different 1.
//按位与运算&
System.out.println(
0
&
0
);
/
/
0
System.out.println(
0
&
1
);
/
/
0
System.out.println(
1
&
1
);
/
/
1
System.out.println(
"==========="
);
/
/
按位或运算符|
System.out.println(
0
|
0
);
/
/
0
System.out.println(
0
|
1
);
/
/
1
System.out.println(
1
|
1
);
/
/
1
System.out.println(
"==========="
);
/
/
异或运算符^
System.out.println(
0
^
0
);
/
/
0
System.out.println(
0
^
1
);
/
/
1
System.out.println(
1
^
1
);
/
/
0
System.out.println(
"==========="
);
Public class Solution { publicint singlenumber (int[] nums) { int result = 0; int n =nums.length; for (int i = 0; i<n; i++) { ^=nums[i]; } return result;} }
136. Single number "Leetcode" XOR operator, algorithm, Java