Topic:
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.
Links: http://leetcode.com/problems/remove-element/
A brush, do not go through the bug free. 19 rows is interesting, because left, the relative position of right is judged before the value judgment, when two subscript coincident, at least one is not in accordance with the second condition, so to determine whether the current value is equal to Val
1 classsolution (object):2 defremoveelement (self, Nums, val):3 """4 : Type Nums:list[int]5 : Type Val:int6 : Rtype:int7 """8 if notNums:9 return0Tenleft =0 Oneright = Len (nums)-1 ALength =Len (nums) - - whileLeft <=Right : the whileLeft < Right andNums[left]! =Val: -Left + = 1 - whileRight > Left andNums[right] = =Val: -Right-= 1 + ifleft = =Right : - returnLeft + 1ifNums[left]! = ValElse Left +Nums[left] =Nums[right] ALeft + = 1 atRight-= 1 - returnLeft
. Remove Element