其他分享
首页 > 其他分享> > LeetCode 27. 移除数组元素

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

 

标签:27,val,nums,int,lenth,移除,LeetCode
来源: https://www.cnblogs.com/Linanjing/p/15306908.html