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 n = A.length; for (int i = n-1; i >= 0; i--) { if (A[i] == elem) { A[i] = A[n-1]; n--; } } return n; } }
No comments:
Post a Comment