只出现一次的数字

原题

  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?算法

题目大意

  给定一个数组,每一个元素都出现2次除了其中的一个,找出只出现一次的数字注意:算法必须是线性时间复杂度,能够不使用额外空间实现吗?数组

解题思路

  使用异或运算。app

代码实现

算法实现类spa

public class Solution {

    public int singleNumber(int[] nums) {

        if (nums == null || nums.length < 1) {
            throw new IllegalArgumentException("nums");
        }


        for (int i = 1; i< nums.length; i++) {
            nums[0] ^= nums[i];
        }
        return nums[0];
    }
}
相关文章
相关标签/搜索