27. Remove Element

Posted jyg694234697

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了27. Remove Element相关的知识,希望对你有一定的参考价值。

Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

 

技术分享图片

 

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

 

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

 

以上

以上是关于27. Remove Element的主要内容,如果未能解决你的问题,请参考以下文章

leetCode #27 remove element

27. Remove Element

#27 Remove Element

27. Remove Element

[Leetcode] 27 Remove Element

27. Remove Element