-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove Element.java
39 lines (37 loc) · 995 Bytes
/
Remove Element.java
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
32
33
34
35
36
37
38
39
class Solution {
public int removeElement(int[] nums, int val) {
int index = 0;
for(int n: nums){
if(n != val){
nums[index++] = n;
}
}
return index;
}
}
// original
// class Solution {
// public int removeElement(int[] nums, int val) {
// int i = 0;
// for(; i < nums.length; i++){
// if(nums[i] == val){
// int j = i+1;
// while(j < nums.length && nums[j] == val){
// j++;
// }
// if(j == nums.length){
// break;
// }
// else{
// swap(nums, i, j);
// }
// }
// }
// return i;
// }
// public void swap(int[] nums, int i, int j) {
// int temp = nums[i];
// nums[i] = nums[j];
// nums[j] = temp;
// }
// }