Compare just once


  • 0
    L

    Re: C++ simple AC solution in 10 lines 21ms
    I think u can optimize your code by just compare rear-1 with i-th location

    class Solution {
    public:
    int removeDuplicates(vector<int>& nums) {
    int n = nums.size(),pos=1; //pos record the index of the last right values [0,pos]
    if(n<=1)
    return n;
    for(int i=2; i < n; i++){
    if(nums[i]!=nums[pos-1]){
    nums[++pos] = nums[i];
    }
    }
    return pos+1;
    }
    };


Log in to reply
 

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