LeetCode之RemoveElement

题目:

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.

分析:

要求,移除具有n个元素的数组中全部指定的数字,返回删除后的数组长度。

看似简单。事实上也能体现一个人的编程水平。

解法1是优化后的,解法2是參照网上的STL解法。记录下来。

代码:


解释一下STL的几个算法。都包括在algorithm中:
  1. random_shuffle(a,a+20);是将数组元素随机打乱。

  2. remove(A,A+n,elem);是移除数组中elem元素,可是并没有把空间缩小,要缩小虚要用erase方法
  3. distance(A,A+n);是计算两个地址间的距离,也就是元素个数
原文地址:https://www.cnblogs.com/cynchanpin/p/7106903.html