【第22期】观点:IT 行业加班,到底有没有价值?

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)
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

LeetCode 27 : Remove Element

LeetCode 27 : Remove Element URL : https://leetcode.com/problems/remove-element/ 方法很简单,使用两个游标 i 和 ...

LeetCode解题报告汇总

LeetCode解题报告: [LeetCode]1.Two Sum - Yoona - 博客频道 - CSDN.NET <a target="_blank" href="http://blog.csdn.net/sunnyyoona/article/details

leetcode 27 remove element

由于自己的算法实在太烂,所以不得不去学好,从最低的leetcode开始刷起,从简单干起。leetcode 27 remove elementGiven an array and a value, re...

[LeetCode]27.Remove Element

【题目】 Given an array and a value, remove all instances of that value in place

LeetCode Weekly Contest 27解题思路

LeetCode Weekly Contest 27解题思路赛题本次周赛主要分为以下4道题: 557 Reverse Words in a String III (3分) 554 Brick Wall...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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