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

(LeetCode)Remove Element --- 去掉指定元素

时间:2016-08-04 10:34:01      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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.

Subscribe to see which companies asked this question


解题分析:先计数,然后再去掉count个的指定val。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def removeElement(self, nums, val):
        count = 0
        for i in xrange(len(nums)):
            if  val == nums[i]:
                count += 1

        for j in xrange(count):
            nums.remove(val)



(LeetCode)Remove Element --- 去掉指定元素

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/52115947

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