LeetCode——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.
中文:给定一个数组和一个数值,去除这个数值全部出现位置,并返回新数组的长度。
元素的顺序能够改变。除了新的长度,你留下什么并不重要。
Java
public static int removeElement(int[] A, int elem) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < A.length; i++) { if (A[i] == elem) continue; list.add(A[i]); } for (int i = 0; i < list.size(); i++) A[i] = list.get(i); return list.size(); }