【LeetCode從零單排】No27.Remove Element

李博Garvin發表於2015-02-12

題目

    

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 next = 0;
    for(int i = 0; i < A.length; i++) {
        if(A[i] != elem) {
            A[next++] = A[i];
        }
    }
    return next;
    }
}



/********************************

* 本文來自部落格  “李博Garvin“

* 轉載請標明出處:http://blog.csdn.net/buptgshengod

******************************************/




相關文章