Remove Duplicates From Sorted Array
Question
Example:
Answer
solution1:
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: #以防止空list所造成的error
return 0
last, i = 0, 1 #关键是明确两个指针的作用,一个是往前不断取值,一个是记录符合条件的值得个数。
while i < len(nums):
if nums[last] != nums[i]:
last += 1
nums[last] = nums[i]
i += 1
return last + 1solution2:this method which I wanna use is not accepted, cause it used extra space, which is not allowed.
Knowledge:
Last updated