-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRemoveElement.java
More file actions
59 lines (53 loc) · 1.51 KB
/
RemoveElement.java
File metadata and controls
59 lines (53 loc) · 1.51 KB
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
51
52
53
54
55
56
57
58
59
package twopointers;
// Source : https://leetcode.com/problems/remove-element/
// Id : 27
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-11-06
// Topic : Array
// Level : Easy
// Other :
// Tips :
// Result : 100.00% 100.00%
public class RemoveElement {
// 100.00% 100.00%
public int removeElement(int[] nums, int val) {
int res = nums.length, head = 0, tail = nums.length - 1;
while (head <= tail) {
if (nums[head] == val) {
res--;
while (nums[tail] == val) {
// head = tail && value = val, it means we finished check,
// and res has already been reduced above while loop
if (tail == head)
return res;
res--;
tail--;
}
nums[head] = nums[tail];
tail--;
}
head++;
}
return res;
}
// practice
// 0ms
public int removeElement1(int[] nums, int val) {
// public int removeElement(int[] nums, int val) {
if (nums.length == 0)
return 0;
int l = 0, r = nums.length - 1;
while (l < r) {
if (nums[l] == val) {
if (nums[r] == val) {
r--;
} else {
nums[l] = nums[r];
r--;
}
} else
l++;
}
return l;
}
}