【LeetCode OJ】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 {
 3 public:
 4     int removeElement(int A[], int n, int elem)
 5     {
 6       for (int i = 0; i < n; ++i)
 7         {
 8         if (A[i] == elem)
 9             {
10             for (int j = i; j < n; ++j)
11                 {
12                     A[j] = A[j+1];
13                 }
14             n--;
15             i--;
16             }
17         }
18     return n;  
19     }
20 };

 

posted @ 2015-04-07 09:24  温布利往事  阅读(322)  评论(0编辑  收藏  举报