数据结构算法操作试题(C++/Python)——删除排序数组中的重复项

文章目录


数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/submissions/

数据结构算法操作试题(C++/Python)——删除排序数组中的重复项

2. 解答

python: 56ms, 12.7MB, 98.03%

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        tmp_index = 1
        if not nums: return 
        for i in range(1, len(nums)):
            if nums[i-1] != nums[i]:
                nums[tmp_index] = nums[i]
                tmp_index += 1
        return tmp_index

其他方法看 leetcode 链接 评论区~