数据结构算法操作试题(C++/Python)——移除元素

时间:2022-07-24
本文章向大家介绍数据结构算法操作试题(C++/Python)——移除元素,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 题目

leetcode 链接:https://leetcode-cn.com/problems/remove-element/

2. 解答

python: 28MS, 10.7MB

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

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