[leetcode] Remove Duplicates from Sorted Array python
博客专区 > ludlows 的博客 > 博客详情
[leetcode] Remove Duplicates from Sorted Array python
ludlows 发表于3年前
[leetcode] Remove Duplicates from Sorted Array python
  • 发表于 3年前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

problem:


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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

solution:

class Solution:
    # @param a list of integers
    # @return an integer
    def removeDuplicates(self, A):
        if len(A) == 0:
            return 0
        n = len(A)
        index = 0
        for i in xrange(n):
            if A[index] != A[i]:
                index += 1
                A[index] = A[i]
        A = A[:index+1]
        return len(A)


共有 人打赏支持
粉丝 0
博文 15
码字总数 4195
×
ludlows
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: