#!/usr/bin/python# -*- coding: utf-8 -*-'''
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.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
'''classSolution(object):defremoveDuplicates(self, nums):"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
index = 0
new_len = 0while index < length:
if index > 0and nums[index] == nums[index - 1]: #判断顺序不要乱换,不然会越界while index < length and nums[index] == nums[index - 1]:
index += 1else:
nums[new_len] = nums[index]
index += 1
new_len += 1
nuns = nums[:new_len]
return new_len
if __name__ == "__main__":
s = Solution()
print s.removeDuplicates([1,1,1,2,2,2,2,22])