2017年11月6日
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
int x=0;
for(auto i:nums)
x^=i;
return x;
}
};
[/cc]