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

27. Remove Element

时间:2016-03-05 23:50:03      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:

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

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

AC代码:

class Solution(object):
    def removeElement(self, nums, val):
        current = 0
        for v in nums:
            if v != val:
                nums[current] = v
                current += 1
        return current

本题和26. Remove Duplicates from Sorted Array的第二种解法几乎一模一样,甚至比那个还简单,这里就不再赘述了。

27. Remove Element

标签:

原文地址:http://www.cnblogs.com/zhuifengjingling/p/5246125.html

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