-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0027RemoveElement.java
41 lines (38 loc) · 1.12 KB
/
_0027RemoveElement.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
package com.heatwave.leetcode.problems;
public class _0027RemoveElement {
static class Solution {
public int removeElement(int[] nums, int val) {
int n = nums.length;
int left = 0, right = n - 1;
while (left <= right) {
while (left < n && nums[left] != val) {
left += 1;
}
while (right >= 0 && nums[right] == val) {
right -= 1;
}
if (left > right) {
break;
}
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
return left;
}
}
static class SolutionSlowFast {
public int removeElement(int[] nums, int val) {
int n = nums.length;
int slow = 0, fast = 0;
while (fast < n) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
}
}