Remove Elements I & II in sorted array
题目描述
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 you leave beyond the new length.
II:
duplicates are allowed at most twice.
解题方法
因为新array后面的value不需要考虑,所以我们只需要array前面的value就可以
Solution
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
length = len(nums)
write_p = 0
for i in range(length):
if i != 0 and nums[i] == nums[i-1]:
continue
nums[write_p] = nums[i]
write_p += 1
return write_p