LintCode 172---删除元素

public class Solution {
    /*
     * @param A: A list of integers
     * @param elem: An integer
     * @return: The new length after remove
     */
    public int removeElement(int[] A, int elem) {
          // write your code here
        if(A.length == 0)
            return 0;
            
        int font = 0;
        int end = A.length-1;
            
        while(font <= end){
                        // Move value to position
            if(A[font] == elem && A[end] != elem){
                // swap
                A[font++] = A[end--];
            }else{
                if(A[font] != elem)
                    font++; 
                if(A[end] == elem)
                    end--;
            }
        }
        return font;
    }
}

猜你喜欢

转载自www.cnblogs.com/cnmoti/p/10828511.html