Merge pull request #2302 from wikicoo/master

添加 0027.移除元素 JAVA版本 相向双指针法(版本二)
This commit is contained in:
程序员Carl
2023-10-15 10:00:49 +08:00
committed by GitHub

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