LeetCode 136. Single Number

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?

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size();i+=2)
            if(nums[i]!=nums[i+1])
               return nums[i];
            
    }
};
posted @ 2018-12-02 22:17  A-Little-Nut  阅读(112)  评论(0编辑  收藏  举报