Remove Element

来源:互联网 发布:哪个清理软件最好 编辑:程序博客网 时间:2024/06/05 17:54

Give an arry 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.

#include<stdio.h>int removeElement(int A[],int length,int elem){        if(length == 0){                return 0;        }        int i = 0;        int j = 0;        for(i = 0; i < length; i++){                if(A[i] > elem){                        continue;                }                A[j] = A[i];                j++;        }        for(int i = j; i < length; i++){                A[i] = 0x0;        }        printf("A[] = ");        for(int i = 0; i < j; i++){                printf("%d",A[i]);        }        printf("\n");        return j;}int main(){        int A[] = {1,2,3,3,4,5,2,3};        int length = sizeof(A)/sizeof(A[0]);        int count = removeElement(A,length,3);        printf("count = ");        printf("%d\n",count);        return 0;}

最终运行结果如图:
这里写图片描述