[LeetCode]27. Remove Element

Success
Details 
Runtime: 52 ms, faster than 8.08% of Python3 online submissions for Remove Element.
Memory Usage: 13.8 MB, less than 6.06% of Python3 online submissions for Remove Element.
 

Submission Detail

113 / 113 test cases passed.
Status: 

Accepted

Runtime: 52 ms
Memory Usage: 13.8 MB
Submitted: 1 minute ago
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        #0
        if val ==None or nums == None:
            return None
        if len(nums) == 0:
            return 0
        #normal
        count = nums.count(val)
        for i in range(count):
            nums.remove(val)
        return len(nums)

 

本来以为这样写简洁一点,结果比直接用remove还慢。。

 

24ms:

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        while val in nums:
            for num in nums:
                if num == val:
                    nums.remove(num)
        return len(nums)

 

posted @ 2019-08-12 10:22  夜歌乘年少  阅读(138)  评论(0编辑  收藏  举报