my C++ solution with a linear runtime complexity and without using extra memory


  • 0
    L

    class Solution {
    public:
    int singleNumber(vector<int>& nums) {
    sort(nums.begin(),nums.end());
    int i=0;
    int temp=nums[i];
    int count=1;
    for(i=1;i<nums.size();i++)
    {
    if(nums[i]==temp)
    count++;
    else
    {
    temp=nums[i];
    if(count!=3)
    break;
    else
    count=1;
    }

        }
        return nums[i-1];
    }
    

    };


  • 1
    L

    sort is nlg(n) not linear


Log in to reply
 

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