您的位置:首页 > 其它

LeetCode 136. Single Number

2016-03-29 03:49 507 查看
Given an array of integers, every elements appears twice expect for one. Find the single one. Do it without using extra memory.

This problem can be solved using "set" or Hash Map. However, it is going to use extra memory space.

// we need to consider using bit manipulation. A ^ A = 0, 0 ^ A = A

// Since in this array, every elements appears twice expect for one. We can use above equations.
int onlyOne(vector<int> input) {
// we need to first check the size of input.
if(input.size() == 0) return 0;
int result = 0;
for(auto it : input) {
result ^= it;
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: