two idea for java solution


  • 0
    L
    • solution1
      use one loop
      public int removeElement(int[] nums, int val){
      int i = 0, j = nums.length - 1;
      while(i <= j){
      if(nums[i] == val){
      nums[i] = nums[j];
      j--;
      }else{
      i++;
      }
      }
      return i;
      }

    • solution2
      use two loop,
      public int removeElement2(int[] nums, int val){
      int i = 0, j = nums.length - 1;
      for(i = 0; i <= j; i++){
      while(nums[i] == val && i <= j){
      nums[i] = nums[j--];
      }
      }
      return (j+1);
      }


Log in to reply
 

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