27. Remove Element

来源:互联网 发布:在哪里可以买淘宝号 编辑:程序博客网 时间:2024/06/01 09:20

27. Remove Element
Difficulty: Easy

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.

给定一个数组和一个值val,移除数组中所有值为val的元素并返回新的数组长度。

方法与26题类似。

int removeElement(int* nums, int numsSize, int val) {    int i,valNum;    valNum=0;    for(i=0;i<numsSize;i++)    {        if(nums[i]==val)            valNum++;        else            nums[i-valNum]=nums[i];    }    return numsSize-valNum;}
0 0
原创粉丝点击