My solution based on selection algorithm using python


  • 1
    L
    class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        s = len(nums)
        if s == 0:
            return 0
        i, j = 0, s - 1
        while i <= j:
            while i <= j and nums[i] != val:
                i += 1
            while i <= j and nums[j] == val:
                j -= 1
            if i <= j:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1
                j -= 1
            else:
                break
        return i

Log in to reply
 

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