[LeetCode] Single Number

来源:互联网 发布:派派刷钻石辅助软件 编辑:程序博客网 时间:2024/06/06 19:21

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的特点就可以了,方法就是——异或!!!

此题是LeetCode中AC率暂时最高的一题。

代码如下:

class Solution {public:    int singleNumber(int A[], int n) {        int res = 0;        for (int i=0; i<n; i++)        {            res = res ^ A[i];        }        return res;    }};


0 0