码迷,mamicode.com
首页 > 其他好文 > 详细

26. Remove Duplicates from Sorted Array

时间:2018-08-27 15:28:41      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:each   inf   .com   div   caller   return   class   not   get   

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

技术分享图片

 

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

 

class Solution(object):
    def removeDuplicates(self, A):
        """
        :type nums: List[int]
        :rtype: int
        """
        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

 

 

以上

26. Remove Duplicates from Sorted Array

标签:each   inf   .com   div   caller   return   class   not   get   

原文地址:https://www.cnblogs.com/jyg694234697/p/9542298.html

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