Leetcode 136 level 1

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?


Note:

Easy one. But I just started to re-use the c++ so I forgot how to use the 'exclusive or' command in C++.

Marked as Level one since I know the algorithm.


Note:

1. The 'exclusive or' command in c++ is '^'

2. The iteration steps in c++ for vector is:

for(std::vector<int>::iterator i=nums.begin();i!=nums.end();++i){}

Answer:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int tmp=0;
        for(std::vector<int>::iterator i=nums.begin();i!=nums.end();++i){
            tmp^=(*i);
        }
        return tmp;
    }
};

猜你喜欢

转载自blog.csdn.net/Shit_Kingz/article/details/79993542