27. 移除元素

class Solution:
    def removeElement(nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        j=0
        for i in range(len(nums)):
            if nums[i] != val:
                nums[j] = nums[i]
                j = j+1
        return j,nums
# test
sol = Solution
result = sol.removeElement(nums =[0,1,2,2,3,0,4,2],val=2)
print(result)

你可能感兴趣的:(27. 移除元素)