Accepted Java Solution


  • 8
    M
    public int removeElement(int[] nums, int val) {
        int start = 0, end = nums.length-1;
        while(start<=end){
            if(nums[start] == val){
                if(nums[end] == val)
                    end--;    
                else{
                    nums[start] = nums[end];
                    start++; end --;
                }
            }else{
                start++;
            }
        }
        return start;
    }

  • 0
     for(int i=0,j=0;i<nums.length&&j<nums.length;)
                      { 
                          if(nums[j]!=val)
                           {
                                if(j-i>0)
                            {
                          f=1;
                                nums[i]=nums[j];
                            nums[j]=-11111;
                                
                            }
                               
                               i++;
                           j++;
                            continue;
                           
                           }
                            else{
                              
                           j++;
                            }
                        
                          
                      }
     
      
                    for(int i=0;i<nums.length;i++){
                        if((nums[i]==val||nums[i]==-11111))
                        return i;
                 
                     }

Log in to reply
 

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