Merge pull request #84 from Joshua-Lu/patch-2

跟新 0027.移除元素 Java版本
This commit is contained in:
Carl Sun
2021-05-14 09:39:43 +08:00
committed by GitHub

View File

@ -126,16 +126,18 @@ Java
```java
class Solution {
public int removeElement(int[] nums, int val) {
int p2 = 0;
for (int p1 = 0; p1 < nums.length; p1++) {
if (nums[p1] != val) {
nums[p2] = nums[p1];
p2++;
// 快慢指针
int fastIndex = 0;
int slowIndex;
for (slowIndex = 0; fastIndex < nums.length; fastIndex++) {
if (nums[fastIndex] != val) {
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
return p2;
}
}
```