Skip to content

Commit 9183693

Browse files
authored
22.RemoveElement-双指针
1 parent 56dab40 commit 9183693

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: RemoveElement.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int removeElement(int[] nums, int val) {
3+
int n = nums.length;
4+
int l = 0;
5+
int r = n - 1;
6+
while (l <= r) {
7+
if (nums[l] == val) {
8+
nums[l] = nums[r];
9+
r--;
10+
} else {
11+
l++;
12+
}
13+
}
14+
return l;
15+
}
16+
}

0 commit comments

Comments
 (0)