LeetCode(27) Remove Element

这道题跟26题的测评方法类似,思路也很简单,就是遍历数组,把与val不相同的数调到前面就行。


下附AC代码:

class Solution:
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        lens = 0
        for i in range(len(nums)):
            if val != nums[i]:
                nums[lens]=nums[i]
                lens+=1
        return lens

猜你喜欢

转载自blog.csdn.net/gzhermit/article/details/78864031