80. Remove Duplicates from Sorted Array II
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
js
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
if (!nums || !nums.length) {
return 0
}
var first = 0
var second = 0
for (let i = 1; i < nums.length; i++) {
if (nums[i] !== nums[second]) {
nums[++second] = nums[i]
first = second
} else if (first === second) {
nums[++second] = nums[i]
}
}
return second + 1
}
py
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
first = second = 0
for num in nums[1:]:
if num != nums[second]:
second += 1
first = second
nums[second] = num
elif first == second:
second += 1
nums[second] = num
return second + 1