标签:
这个题目没有动手实践,只是想了个思路,结果一看讨论区的代码瞬间感觉,我想的太复杂了。ps:有点想不明白,既然是要移除元素,为何不留下一个不含删除元素的纯净数组。
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.
public class Solution { public int removeElement(int[] A, int elem) { int startPosition = 0; for (int i = 0; i < A.length; i++) { if (A[i] != elem) { A[startPosition] = A[i]; startPosition++; } } return startPosition; } }
标签:
原文地址:http://blog.csdn.net/wj512416359/article/details/42065737