LeetCode 27. 移除数组元素

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

LeetCode 27. 移除数组元素 by Python3

1 class Solution:#双指针法实现,推荐
2     def removeElement(self, nums: List[int], val: int) -> int:
3         i, n = 0, len(nums)
4         for j in range(n):
5             if nums[j] != val:
6                 nums[i] = nums[j]
7                 i += 1
8         return i

 

class Solution:#自己的暴力解,烂
    def removeElement(self, nums: List[int], val: int) -> int:
        i = 0
        lenth = len(nums)
        while i < lenth:
            if nums[i] == val:
                del nums[i]
                lenth -= 1
            else:
                i += 1
        return lenth

 

上一篇:写学习abcde的简单AI(C++实现)


下一篇:如何用程序随机出游戏王180张不重复的卡片