標籤:sys blog 數字 using nbsp integer 數組 ice length
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
題目分析:數字每一位存在數組中,每個數字承兌出現,只有一個數字只出現一次,我們知道異或運算子相同為0不同為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 { public int singleNumber(int[] nums) { int result = 0; int n =nums.length; for (int i = 0; i<n; i++) { result ^=nums[i]; } return result; }}
136. Single Number【LeetCode】異或運算子,演算法,java