How to solve this problem if the array is not sorted?


  • 0
    T

    well,I didn't notice that the array is sorted,this is my java code:

    public class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length<2){
            return nums.length;
        }
        int k=nums.length;
        byte[] count1=new byte[10000];
        byte[] count2=new byte[10000];
        int temp1,temp2,temp3;
        temp3=nums[0];
        for(int i=0;i<k;i++){
            if(nums[i]>0){
                temp1=nums[i]/8;
                temp2=nums[i]%8;
                if((count1[temp1]&(1<<temp2))!=0){
                    nums[i]=temp3;
                }else{
                    (count1[temp1])|=(1<<temp2);
                }
            }else{
                int temp=nums[i]*(-1);
                temp1=temp/8;
                temp2=temp%8;
                if((count2[temp1]&(1<<temp2))!=0){
                    nums[i]=temp3;
                }else{
                    count2[temp1]|=(1<<temp2);
                }
            }
        }
        int i=1;
        for(int j=1;i<k;i++){
            while((j<k)&&(nums[j]==temp3)){
                j++;
            }
            if(j>=k){
                break;
            }
            nums[i]=nums[j];
            j++;
        }
        return i;
    }
    

    }


Log in to reply
 

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