C++ solution easy to understand


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

  • 0
    S

    linear runtime complexity. The sort is O(nlogn)


Log in to reply
 

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