【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】

【136-Single Number(仅仅出现一次的数字)】


【LeetCode-面试算法经典-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?


题目大意

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

解题思路

  使用异或运算。

代码实现

算法实现类

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];
    }
}

评測结果

  点击图片。鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

这里写图片描写叙述

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47745389

原文地址:https://www.cnblogs.com/yangykaifa/p/7338541.html