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
commit eabe8dbd36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

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