C++ 16ms code, O(N)


  • 0
    Z
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int size = nums.size();
            if(size == 0) return 0;
            int j = 0;
            int i = 1;
            int count  =1;
            while(i<size)
            {
                if(nums[i]==nums[j] && count==1)
                {
                    nums[++j]=nums[i];
                    count = 2;   
                }
                else if(nums[i]!=nums[j])
                {
                    nums[++j]=nums[i];
                    count = 1;
                }
                ++i;
            }
            return j+1;
            
        }
    };

Log in to reply
 

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