Linear time, no more space, my ruby solution


  • 0
    W
    def remove_element(nums, val)
        return 0 if nums.nil? || nums.size == 0
        head = 0
        tail = nums.size - 1
        while head <= tail
          while nums[tail] == val
            tail -= 1
          end
          if nums[head] == val && head < tail
            nums[head] = nums[tail]
            tail -= 1
          end
          head += 1
        end
        # print nums[0..tail]
        tail + 1
      end

Log in to reply
 

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