forked from algorhythms/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (29 loc) · 697 Bytes
/
Copy pathSolution.java
File metadata and controls
31 lines (29 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package RemoveElement;
/**
* User: Danyang
* Date: 1/26/2015
* Time: 10:58
*
* 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 {
/**
* Put it to back
* @param A
* @param elem
* @return
*/
public int removeElement(int[] A, int elem) {
int e = A.length-1;
for(int i=0; i<=e; ) {
if(A[i]!=elem)
i++;
else {
int t = A[i]; A[i] = A[e]; A[e] = t;
e--;
}
}
return e+1;
}
}