leetcode-27-Remove Element

原创 2016年06月01日 11:17:10

题目:Given an array and a value, 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 in place with constant memory.

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

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

题意:去掉list中值等于val的元素,其他若干元素依次保存在list前若干位置。不用新的空间,使用连续内存。代码和上一题类似。

python AC 56ms。

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        k=len(nums)
        if k==0: 
            return None
        p=0
        for i in range(k):
           if nums[i]!=val:
               nums[p]=nums[i]
               p+=1
        return p
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode_27_Remove Element

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41577997 Remove Element Given ...

LeetCode_27---Remove Element

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

LeetCode笔记:27. Remove Element

固定内存下移除数组中特定的数字

Leetcode - Array - 27. Remove Element(第一道题)

1.Problem description Given an array and a value, remove all instances of that value in place and re...

LeetCode 27_Remove Element

这个题亦不是很难,并没有太多可说的,我们尽快结果了它吧。 题目也是要从数组中移除元素的,但算法还是有点儿小差别的,算是两个不同的小技巧吧。看题 Given an array and a v...

LeetCode(27)Remove Element

题目如下 Given an array and a value, remove all instances of that value in place and return the new leng...

[leetcode]27.Remove Element(Java)

测试地址:https://leetcode.com/problems/remove-element/#/description Given an array and ...

leetcode 27. Remove Element

题目描述: Given an array and a value, remove all instances of that value in place and return the new l...

【leetcode c++】27 Remove Element

Remove Element Given an array and a value, remove allinstances of that value in place and return th...
  • hqq39
  • hqq39
  • 2015-06-22 00:48
  • 304

leetcode 27 Remove Element

27. Remove Element Given an array and a value, remove all instances of that value in place and ret...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)