-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
33 lines (30 loc) · 901 Bytes
/
Solution.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
package ds.pointer.leetcode27;
/**
* 移除元素
* LeetCode 27 https://leetcode-cn.com/problems/remove-element/
*
* @author yangyi 2020年12月16日16:23:42
*/
public class Solution {
public int removeElement(int[] nums, int val) {
if (nums == null || nums.length == 0) {
return 0;
}
int slow = 0, fast = 0;
while (fast < nums.length) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
public static void main(String[] args) {
int[] res1 = new int[]{3, 2, 2, 3};
int[] res2 = new int[]{0, 1, 2, 2, 3, 0, 4, 2};
Solution removeElement = new Solution();
System.out.println(removeElement.removeElement(res1, 3));
System.out.println(removeElement.removeElement(res2, 2));
}
}