Two pointers solution in python


  • 1
    class Solution(object):
        def removeElement(self, nums, val):
            low, high = 0, len(nums) - 1
            while 1:
                while low < len(nums) and nums[low] != val:
                    low += 1
        
                while high >= 0 and nums[high] == val:
                    high -= 1
        
                if low >= high:
                    break
        
                nums[low], nums[high] = nums[high], nums[low]
            return low

  • 0
    R

    you don't seem to need "r", just use "low"


  • 0

    Exactly, thank you!


Log in to reply
 

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