mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-05 22:59:31 +08:00
Update
This commit is contained in:
@ -119,35 +119,6 @@ public:
|
||||
* 时间复杂度:O(n)
|
||||
* 空间复杂度:O(1)
|
||||
|
||||
```CPP
|
||||
/**
|
||||
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
|
||||
* 时间复杂度:O(n)
|
||||
* 空间复杂度:O(1)
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
int removeElement(vector<int>& nums, int val) {
|
||||
int leftIndex = 0;
|
||||
int rightIndex = nums.size() - 1;
|
||||
while (leftIndex <= rightIndex) {
|
||||
// 找左边等于val的元素
|
||||
while (leftIndex <= rightIndex && nums[leftIndex] != val){
|
||||
++leftIndex;
|
||||
}
|
||||
// 找右边不等于val的元素
|
||||
while (leftIndex <= rightIndex && nums[rightIndex] == val) {
|
||||
-- rightIndex;
|
||||
}
|
||||
// 将右边不等于val的元素覆盖左边等于val的元素
|
||||
if (leftIndex < rightIndex) {
|
||||
nums[leftIndex++] = nums[rightIndex--];
|
||||
}
|
||||
}
|
||||
return leftIndex; // leftIndex一定指向了最终数组末尾的下一个元素
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
## 相关题目推荐
|
||||
|
Reference in New Issue
Block a user