[Leetcode] 27. Remove Element

来源:互联网 发布:js array join方法 编辑:程序博客网 时间:2024/06/11 08:44

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) {        if(A == null || A.length == 0) return 0;        int start = 0;        int end = A.length - 1;        while(start <= end){            if(A[start] == elem){                A[start] = A[end];                end--;            } else {                start++;            }        }        return end + 1;    }}


0 0