mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2302 from wikicoo/master
添加 0027.移除元素 JAVA版本 相向双指针法(版本二)
This commit is contained in:
@ -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:
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user