leetcode:Remove Element
code from: https://github.com/soulmachine/leetcode
class Solution { public: int removeElement(int A[], int n, int elem) { return distance(A,remove(A,A+n,elem)); } };
又是STL一句话搞定。distance之前已经学过了,正好复习一下:
distance(InputIterator first, InputIterator last), 返回的是两个迭代器的距离。
remove的说明是这样的:
template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
Transforms the range [first,last)
into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range.