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

LeetCode 27 Remove Element

时间:2015-03-30 16:28:15      阅读:157      评论: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.

题目翻译:给定一个数组A和一个值elem,删除所有的elem,并返回新的数组长度。元素的顺序可以改变。

C语言版:

int removeElement(int A[], int n, int elem) {
    int j = 0;
    for(int i = 0; i < n; i++){
        if (A[i] != elem)
            A[j++] = A[i];
    }
    return j;
}

Python版:

class Solution:
    # @param    A       a list of integers
    # @param    elem    an integer, value need to be removed
    # @return an integer
    def removeElement(self, A, elem):
        j = 0
        for i in A :
            if i != elem :
                A[j] = i
                j += 1
        return j


LeetCode 27 Remove Element

标签:

原文地址:http://blog.csdn.net/hpuhjl/article/details/44751805

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