LeetCode 27. 移除数组元素

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了LeetCode 27. 移除数组元素脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

https://leetcode-cn.COM/PRoblems/remove-element/

LeetCode 27. 移除数组元素 by Python3

@H_777_5@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

 

脚本宝典总结

以上是脚本宝典为你收集整理的LeetCode 27. 移除数组元素全部内容,希望文章能够帮你解决LeetCode 27. 移除数组元素所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。