Remove Duplicates from Sorted Array

Source: Internet
Author: User

Topic:

Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.

Do the allocate extra space for another array, and you must does this on place with constant memory.

For example,
Given input array nums = [1,1,2] ,

Your function should return length = 2 , with the first of the elements of nums being and 1 2 Respectivel Y. It doesn ' t matter what are you leave beyond the new length.

Code:

There is no difficulty in understanding the topic, that is, the number of the order, remove the repetition, there are still a few.

However, the topic requires that memory cannot be increased, that is, you cannot create a new data structure, but you can delete an existing list:)

thus

#-*-Utf-8-*-
#Remove Duplicates from Sorted Array
Class solution (Object):
def removeduplicates (self, nums):
Total_length = Len (nums)
For Index,value in Enumerate (nums):
If index = = Total_length-1:break

#遇到后一个元素和该元素一样, delete the element until it is not the same
While Nums[index]==nums[index+1]:
List.remove (Nums, Nums[index])
#index = index+1
Total_length = total_length-1
If index = = Total_length-1:break
#print Nums
Return total_length

If __name__== "__main__":
NUMS=[-49,-49,-48,-48,-47,-47,-47,-46,-46,-46,-46,-44,-44,-44,-42,-42,-41,-41,-40,-40,-39,-39,-39,-38,-37,-37, -36,-36,-35,-35,-35,-34,-34,-32,-32,-31,-30,-29,-28,-28,-27,-27,-27,-26,-26,-25,-25,-25,-24,-23,-22,-21,-21,- 21,-20,-20,-20,-20,-20,-18,-18,-17,-17,-16,-16,-15,-15,-15,-14,-13,-13,-11,-10,-10,-9,-9,-9,-9,-9,-9,-7,-6,-6, -6,-5,-5,-5,-4,-3,-3,-1,-1,-1,-1,- 1,1,3,3,5,5,5,5,6,6,7,8,8,8,8,9,9,10,10,13,14,14,14,15,15,15,16,17,18,18,19,19,20,20,20,21,21,21,22,23,23,24,25,25,25,25 , 26,26,26,26,26,27,27,27,27,27,27,27,28,29,29,30,30,30,30,31,31,32,32,34,35,37,38,39,39,39,41,43,44,44,44,45,45,45,46,48 , 49,49,49,49,50]
A = solution ()
Print "Result:%d"% (A.removeduplicates (nums))
Print Nums

Implementation is achieved, but with more than 500 ms~~. There's still a lot of room for improvement!

Baidu a bit, found himself guilty again, a little change, you can improve the efficiency of a wide range:

def removeduplicates (self, nums):
#total_length = Len (nums)
If Len (nums) = = 0:return None
Result=1
For index in range (0,len (nums)-1):
#判断后面的元素不等于前面的元素, Count +1, and assign to the previous segment of the array
If nums[index+1]! = Nums[index]:
Nums[result] = nums[index+1]
Result=result+1
return result

76ms OH:

Remove Duplicates from Sorted Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.