Premiumlab  

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

 

Restatement

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.

 

 

Analysis

We can apply the fast-and-slow-pointer technique to solve this problem.  One slower pointer i denotes the location of the comparable number,  while the fast pointer j traverses the rest of rest of the array to find duplicates. 

 

The idea is to compare the first character with the second character, if the second character is not  duplicated, then  swap the first character with the second, however, if the second character is not  duplicated, advance the fast pointer j to the next character and swapping repeatedly until it reaches the end. Then move the slower pointer i from the first character to the second character, and compare the second character with the third, forth, fifth character and so on. 

 

Finally, the new array has the length of i+1. 

 

Solution

class Solution:
    # @param a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        if len(A) == 0:
            return 0
        j = 0
        for i in range(0, len(A)):
            if A[i] != A[j]:
                A[i], A[j+1] = A[j+1], A[i]
                j = j + 1
        print j + 1
        return j+1
nums=[1,2,3,3,4,4,8]
obj= Solution()
obj.removeDuplicates(nums)
print(nums)

 

 

Notes

 

1. 4 space indentation after line def

2. Return should be aligned with the line after def

2. Single out the condition where the length of the array is 0.

 

 

My ans 

class Solution(object):
    def removeDuplicates(self, nums):
        i=0
        j=0
        if len(nums) == 0:
            return 0
        while(i<len(nums)):
            j=i
            while(j<len(nums)-1):
                if nums[i] == nums[j+1]:
                    nums.remove(nums[j+1])
                else:
                    j=j+1
            i=i+1
        return(len(nums))
nums=[1,1,3,3,3,3,3,0,0,0,0,2,2,2]
obj= Solution()
obj.removeDuplicates(nums)
print(nums)

 

Diagnose

Time Limit Exceeded

My answer exceeds the time limit for the following two reason:

 

1. Unneccessary loops

 

There are two while loops in my codes, which are i and j loop. 

while(i<len(nums)):
while(j<len(nums)-1):

 

However, in the solution, there is only one for loop. 

 

for i in range(1,len(A)):

 

2. Ussage of remove fuction

 

According to the problem, "It doesn't matter what you leave beyond the new length." Thus, it is unnecessary to use remove funtion.  

 

Traps

1. Every time the fast pointer j circles, it begins with the place of the slow pointer i, rather than 0. 

2. After the remove function is executed, the program should go to the while judgement statement, rather than add one to j, because it does not hold true for the condition where several duplicates appear in a row.  

 

Other notable solutions

 

class Solution:
    # @param a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        if not A:
            return 0

        newTail = 0

        for i in range(1, len(A)):
            if A[i] != A[newTail]:
                newTail += 1
                A[newTail] = A[i]

        return newTail + 1

 

posted on 2017-02-05 11:45  Premiumlab  阅读(131)  评论(0编辑  收藏  举报