您的位置:首页 > 理论基础 > 数据结构算法

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

2017-08-10 11:54 621 查看

【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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐