标签:moved data- sort param beyond mod for utf-8 his
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.
#-*- coding:utf-8 -*- class Solution: # @param {integer[]} nums # @return {integer} def removeDuplicates(self, nums): l = len(nums) if l == 0: return 0 index = 0 i = 1 nums[index] = nums[0] while i<l: if nums[index] != nums[i]: index += 1 nums[index] = nums[i] i += 1 return index+1 if __name__=="__main__": s = Solution() print s.removeDuplicates(x)
Remove Duplicates from Sorted Array [Python]
标签:moved data- sort param beyond mod for utf-8 his
原文地址:http://www.cnblogs.com/llguanli/p/7159790.html