給出2*n + 1 個的數字,除其中一個數字之外其他每個數字均出現兩次,找到這個數字。
給出 [1,2,2,1,3,4,3],返回 4
public class Solution {
public static int singleNumber(int[] arr){
int res =0;
for(int i =0;i<arr.length;i++){
res = res ^ arr[i];
}
return res;
}
}
給出2*n + 1 個的數字,除其中一個數字之外其他每個數字均出現兩次,找到這個數字。
給出 [1,2,2,1,3,4,3],返回 4
public class Solution {
public static int singleNumber(int[] arr){
int res =0;
for(int i =0;i<arr.length;i++){
res = res ^ arr[i];
}
return res;
}
}