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

Remove Duplicates from Sorted Array [Python]

时间:2015-06-03 17:40:32      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:leetcode   python   



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]

标签:leetcode   python   

原文地址:http://blog.csdn.net/nersie/article/details/46348069

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