LeetCode 136. Single Number

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?

解析:
给定一组整数,除了一个数字出现一次之外,其余所有数字均出现了两次,找到这个数字。算法要求O(n)复杂度。
题目要求线性复杂度,直接采用异或操作处理。
[cc lang=”C++”]
class Solution {
public:
int singleNumber(vector& nums) {
int x=0;
for(auto i:nums)
x^=i;
return x;
}
};
[/cc]

Add a Comment

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据