single num--唯一一个只出现一次 的数

来源:互联网 发布:mysql不等于查询 编辑:程序博客网 时间:2024/05/22 03:12

问题:

Given an array of integers, every element appears twice except for one. Find that single one.

解答:

利用异或,相同为0, 相异为1,0异或任何数不变。


代码:

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

还有一个题 其他数出现了三次,一个数出现了一次的题目 还没做

0 0
原创粉丝点击