Remove Element

来源:互联网 发布:文网文增加域名 编辑:程序博客网 时间:2024/06/05 23:09

Remove Element

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.

解题思路:

题目大意,删除数组中的指定元素。实现方法比较简单,通过指针,可以在O(n)时间复杂度内解决。

 

Code:

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


 

0 0
原创粉丝点击