Q:
Given an array of integers, every element appears twice except for one. Find that single one.
找出数组中的单身狗。
A:
public class Solution {
public int singleNumber(int[] nums) {
int m = 0;
for(int i = 0; i < nums.length; i++)
{
m = m ^ nums[i];
}
return m;
}
}
总结:
主要用到了元素异或,0^N=N,N^N=0;位运算,直接对整数在内存中的二进制位进行操作。