My solution is trying to sort the list firstly, and then make two pointers: one starts from leftmost and one starts from rightmost, they stop when the element equals the target value, in the end sum up the elements in the left and right.
I try to run this solution but if fails with a test case giving list [4,5] and target val = 4, the output should be 1 and so does this solution, I don't understand where the problem is with my solution. Anyone has a hint? Thanks in advance!!
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ if val not in nums: return len(nums) nums.sort() left = 0 right = len(nums) - 1 while left <= right: if nums[left] < val: left += 1 elif nums[right] > val: right -= 1 elif nums[left] == val and nums[right] == val: leftelement = left rightelement = len(nums) - 1 - right return leftelement + rightelement break