码迷,mamicode.com
首页 > 编程语言 > 详细

Remove Duplicates from Sorted Array [Python]

时间:2017-07-13 12:39:36      阅读:159      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!