Remove Element

来源:互联网 发布:在淘宝免费买东西app 编辑:程序博客网 时间:2024/06/05 01:54

Remove Element

 Total Accepted: 7901 Total Submissions: 24070

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.


The tricky part is unless check discussion, you don't know what the question really means

public class Solution {    public int removeElement(int[] A, int elem) {       int len = A.length;       if (A==null){           return 0;       }       int i = 0;       int count = 0;        while(i<len){            if(A[i] == elem){                i++;                count++;            }            else{                A[i-count] = A[i];                i++;            }        }        return A.length-count;    }}



0 0