Sunday, May 10, 2015

Remove Element

来源:Leetcode

原帖:http://oj.leetcode.com/problems/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.


代码:
 class Solution {  
 public:  
   int removeElement(int A[], int n, int elem) {  
     int start = 0; // start: position that will be filled  
     for (int i = 0; i < n; ++i) { // i: current position  
       if (A[i] != elem) {  
         A[start++] = A[i];                    
       }  
     }    
     return start;  
   }  
 };  

No comments:

Post a Comment