Leetcode 26) Remove Duplicates from Sorted Array

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

image

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length <=1){
            return nums.length;
        }
        int i=1;
        int prev= nums[0];
        int modiInd =0;
        while(i<nums.length){
            if(prev==nums[i]){
                modiInd++;
            }else{
                nums[i-modiInd]=nums[i];
            }
            prev=nums[i];
            i++; 
        }
        return i-modiInd;
    }
}

다른 답안

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int i = 0;
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }
}

© 2018. All rights reserved.

Powered by Hydejack v8.5.2