2ms Java Solution


  • 0
    C
    public class Solution {
        public int removeDuplicates(int[] nums) {
            int [] a = new int [1000];
            int [] b = new int [1000];
            int j = 0;
            for(int i = 0; i < nums.length; i++){
                if(nums[i] >= 0){
                    if(a[nums[i]]< 2){
                        a[nums[i]]++;
                        nums[j] = nums[i];
                        j++;
                    }
                }else{
                    int tmp = -nums[i];
                    if(b[tmp] < 2){
                        b[tmp]++;
                        nums[j] = nums[i];
                        j++;
                    }
                }
                
            }
            return j;
        }
    }

  • 0

    I don't think we are allowed to create another array.


Log in to reply
 

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