[Lintcode]100. Remove Duplicates from Sorted Array
100. Remove Duplicates from Sorted Array
Description
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
Example
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
我的代码
class Solution:
"""
@param: nums: An ineger array
@return: An integer
"""
def removeDuplicates(self, nums):
# write your code here
if nums == []:
return 0
f = 1
while f!=len(nums):
if nums[f] == nums[f-1]:
nums.pop(f)
else:
f += 1
return f
思路:
同172. Remove Element
时间复杂度O(n)
出错:1
考虑了一个元素,却没考虑空的情况