Simple Java Solution with one pointer


  • 0
    A

    /**
    ***This is method just needs one int ,and needn't any other value
    ***The main core is the i is much fast increasing than num, so I save the value in nums [num-1].
    **/

    public class Solution {
    public int removeDuplicates(int[] nums) {
    int num=1,len=nums.length;
    if(len==0||len==1)
    {
    return len;
    }
    for(int i=1;i<nums.length;i++)
    {
    if(nums[num-1]!=nums[i]) //if not the same,just save the different number in the original nums array
    {
    num++;
    nums[num-1]=nums[i];
    }
    }
    return num;

    }
    

    }


Log in to reply
 

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