和大神们学习每天一题(leetcode)-Remove Element

来源:互联网 发布:https 隐藏index.php 编辑:程序博客网 时间:2024/06/12 00:53

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.

class Solution {public:int removeElement(int A[], int n, int elem) {int nFinalLen = 0;for (int nTemp = 0; nTemp < n; nTemp++){if (A[nTemp] != elem){A[nFinalLen] = A[nTemp];nFinalLen++;}}return nFinalLen;}};


0 0