27. 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.

链接: http://leetcode.com/problems/remove-element/

题解: 数组去除指定元素。 Time Complexity - O(n), Space Complexity - O(1)

public class Solution {

    public int removeElement(int[] A, int elem) {

        if(A == null || A.length == 0)

            return 0;

        int count = 0;

        

        for(int i = 0; i < A.length; i++){

            if(A[i] != elem)

                A[count ++] = A[i];

        }

        

        return count;

    }

}

 

你可能感兴趣的:(element)