2.1.23—线性表—Single Number

来源:互联网 发布:网络歌手袁晓婕诈骗 编辑:程序博客网 时间:2024/06/07 02:12
描述
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?

#include<iostream>using namespace std;int main(){const int n = 11;int a[n] = { 1, 3, 5, 7, 9, 7, 5, 3, 9, 1, 4 };int res = 0;for (int i = 0; i < n; i++)res = res^a[i];cout << res << endl;}