Easy C++ solution


  • -1
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int size = nums.size();
            if(size == 1) return nums[0];
            sort(nums.begin(), nums.end());
            if(nums[0]!=nums[1]) return nums[0];
            if(nums[size-1]!=nums[size-2]) return nums[size-1];
            for(int i=0; i<size-1; i+=2){
                if(nums[i]!=nums[i+1]) return nums[i];
            }
            return -1;
        }
    };
    

  • 0
    B
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.