Single Number(Java代码)
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的变量,和数组中每一个数异或,得到只出现一次的数。
public class Solution { public int singleNumber(int[] A) { int number = 0; for (int i = 0; i < A.length; i++) { number = number ^ A[i]; } return number; } }