My Python Solution, swap the elements


  • 0
    V

    swap the element if it's equal to "val"

    class Solution(object):
        def removeElement(self, nums, val):
            """
            :type nums: List[int]
            :type val: int
            :rtype: int
            """
            i, j = 0, 0
            for j in range(len(nums)):
                if nums[j] != val:
                    nums[i], nums[j] = nums[j], nums[i]
                    i += 1
            return i
    

Log in to reply
 

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