A simple and easy O(n) c++ code(transverse and jump through duplicates)


  • 0
    G
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int start=1,N = nums.size();
            if(N<=1) return N;
            for(int i=1;i<nums.size();i++) {
                if(nums[i]!=nums[i-1]){
                    nums[start]=nums[i];
                    start++;
                }
            }
            return start;
        }
    };

Log in to reply
 

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