leetcode-136-Single Number

来源:互联网 发布:windows gvim配置模板 编辑:程序博客网 时间:2024/05/22 09:46

问题

题目:[leetcode-136]

思路

位运算,利用xor的性质即可。a^a = 0(additive inverse)

代码

class Solution {public:    int singleNumber(vector<int>& nums) {        int sz = nums.size();        if(!sz) return 0;        int ret = nums[i];        for( int i = 1; i < sz; ++i )        {            ret ^= nums[i];        }        return ret;    }};
0 0