添加 0027.移除元素 JAVA版本 相向双指针法(版本二)

This commit is contained in:
guyinfei
2023-08-08 16:02:54 +08:00
parent bf51d4730d
commit eb75312e19

View File

@ -197,6 +197,26 @@ class Solution {
} }
``` ```
```java
// 相向双指针法(版本二)
class Solution {
public int removeElement(int[] nums, int val) {
int left = 0;
int right = nums.length - 1;
while(left <= right){
if(nums[left] == val){
nums[left] = nums[right];
right--;
}else {
// 这里兼容了right指针指向的值与val相等的情况
left++;
}
}
return left;
}
}
```
### Python ### Python