"Leetcode 26" Remove duplicates from Sorted Array (Python) __python

Source: Internet
Author: User
Tags array length set set

Given a sorted array, remove the duplicates in-place such so each element appear only once and return the new length.
Do not allocate extra spaces for another array, and you must does this by modifying the input array in-place with O (1) Extra memo Ry.
Topic Analysis: given a sorted array. Requires that the number of duplicates be deleted, and then returns the new array length that is sorted out.
method One: train of thought: This problem is the simplest one that I have done, the time complexity is O (n). Do not make redundant explanations, directly on the code. Code: (Can be submitted)

Class Solution:
    def removeduplicates (self, Nums): ""
        : Type Nums:list[int]
        : rtype:int
        "" A=0   #用做计数
        length=len (nums)
        if length<2: return
            length to
        i in range (1,length):
            if Nums[a]!=nums[i]:
                a+=1
                nums[a]=nums[i]
        a+=1 return
        A

Method Two: focus on: This is submitted to see the other great God's writing, it is really cool to coax ah, the beginning did not understand, the time complexity is much lower than mine is O (1) Ah. Put it up for everyone to enjoy, I also by the way to make a simple explanation (self-understanding, there are errors please correct)

Class Solution:
    def removeduplicates (self, Nums): ""
        : Type Nums:list[int]
        : rtype:int
        "" Nums[:]=sorted (Set (nums))  
        "This is the difference between the great God and me, the other people's code as long as one line can be."
Actually, I think we should add another line back to Len (Nums) because the title asks you to return a length.
but I found that Leetcode's test for this problem is to look at the array you're dealing with. Did not look at his length is how much,
so return len (nums) dispensable. It's just a matter of asking the question here. '''

An analysis of this unique line of code: First Look at the most inner bracket, that is, set (nums) #set这个方法是进行一个 (unordered, not repetitive) processing, this step directly removes all the duplicate elements in the Nums , but upset the order in which they were sorted, and turned the list into tuples (note that the parentheses in the example below can differentiate between tuples and lists). Let's look at a little plum from someone else's blog:

>>> x = set (' spam ')  
>>> y = set ([' H ', ' A ', ' m '])  
>>> x, y  
(set ([' A ', ' P ', ' s '), ' m ']), set ([' A ', ' h ', ' m '))  


>>> X & y # intersection  
set ([' A ', ' m '])  

>>> x | y # and set set  
([' A ', ' P ', ' s ', ' h ', ' m ']  

>>> x-y set  
set ([' P ', ' s '])  

2. The second bracket list (...) #将我在上一步中提到的元组 (the side effect of set) is changed back to the form of a listing.
3. To the outermost bracket the sorted () #返回一个经过排序的新列表, does not overwrite the previous list. Here to review the difference between sort () and sorted ():

>>>a=[3,2,1,4,5]
>>>b=sorted (a)
>>>a,b
[3,2,1,4,5],[1,2,3,4,5]
> >>a.sort ()
>>>a
[1,2,3,4,5]

4. Attach the results to nums[:] #此时的nums就变成了一个无重复并以排好序的数组.

Summary: when looking at the program of the Great God, review the previous knowledge and learn about the use of set (which I did not seem to have seen in the course of my previous study). Since the understanding of other people's procedures, after the problem, we should try to improve their code quality, to learn from the great God, can not just look at it. To learn from others.

Related Article

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.