【Leetcode】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.

 1 class Solution {
 2 public:
 3     int removeElement(int A[], int n, int elem) {
 4         int i = 0;
 5         for (int j = 0; j < n; ++j) {
 6             if (A[j] != elem) {
 7                 A[i++] = A[j];
 8             }
 9         }
10         return i;
11     }
12 };
View Code

 

posted @ 2014-03-17 22:44  小菜刷题史  阅读(83)  评论(0编辑  收藏  举报