-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem27.java
50 lines (46 loc) · 1.19 KB
/
Problem27.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
40
41
42
43
44
45
46
47
48
49
50
class Problem27 {
public static void main(String[] args) {
int[] nums = {0,1,2,2,3,0,4,2};
nums = new int[]{3,2,2,3};
//nums = new int[]{3};
int value = 2;
int len = new Problem27().removeElement(nums, value);
System.out.println(len);
}
public int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}
public int removeElement1(int[] nums, int val) {
int i = 0;
while (i < nums.length && nums[i] != val) {
i++;
}
int j = i;
while (j < nums.length && nums[j] == val){
j++;
}
while ( j < nums.length) {
if (nums[j] != val) {
nums[i] = nums[j];
nums[j] = val;
while (nums[i] != val) {
i++;
}
}
j++;
}
for (i = 0; i < nums.length; i++) {
if (nums[i] == val) {
return i;
}
}
return i;
}
}