首页 > 技术文章 > 【LeetCode OJ】Remove Element

xujian2014 2015-04-07 09:24 原文

题目: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 };

 

推荐阅读