leetcode27

原创 2016年06月01日 15:35:50

Remove Element

*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.*

方法1:

class Solution(object):

    def removeElement(self,nums, val):
        while 1:
            try:
                nums.pop(nums.index(val))
            except:
                return len(nums)

方法2:

class Solution(object):

    def removeElement(self,nums, val):
        i=0
        for j in range(len(nums)):
            if nums[i]==val:
                nums.pop(i)
            else:
                i+=1
        return len(nums)
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode27]Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen...

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. ...

leetcode27题 题解 翻译 C语言版 Python版

27. Remove Element Given an array and a value, remove all instances of that value in place and ...

LeetCode笔记:27. Remove Element

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

[C++]LeetCode: 27 Implement strStr()

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or ...

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] Remove Duplicates from Sorted Array II [27]

移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次。 这个题类似Remove Duplicates from Sorted Array,第一个想法很直接就是计数,超过2次的就忽略,依据这个思...

LeetCode 27_Remove Element

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

leetcode题解-26. Remove Duplicates from Sorted Array && 27. Remove Element

Remove Duplicates from Sorted Array,题目:Given a sorted array, remove the duplicates in place such tha...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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