码迷,mamicode.com
首页 > 其他好文 > 详细

27. Remove Element

时间:2018-08-31 12:34:11      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:http   integer   lis   org   another   input   turn   use   cat   

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

标签:http   integer   lis   org   another   input   turn   use   cat   

原文地址:https://www.cnblogs.com/jyg694234697/p/9565128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!