Title: |
Remove Element |
Pass Rate: |
32.6% |
Difficulty: |
Simple |
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 are you leave beyond the new length.
I do not know whether it is my understanding of the problem, or the topic is not clear, according to the return of the request, only to remove the same element with Elem after the length of the array to convert to a new array what is the meaning of it? I feel that the direct statistics of the same elements as elem and then the length of a array minus is the length of the conversion, but after submission is not prompt. I don't know, either. So I switched over, two pointers to the operation, one is I, the other is J. I is to follow the sequence, if elem= the current position of the element, J do not go, I continue to go, so that the same as elem to cover off, the last J is the length of the new array.
1 Public classSolution {2 Public intRemoveelement (int[] A,intelem) {3 intLen=a.length,j=0;4 for(inti=0;i<len;i++){5 if(a[i]!=elem) {6a[j]=A[i];7J + +;8 }9 }Ten returnJ; One } A}
Leetcode------Remove Element