Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 343 Bytes

27.md

File metadata and controls

20 lines (17 loc) · 343 Bytes

27. 移除元素

同向双指针

时间复杂度:O(n)。

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int l = 0;
        for (int r = 0; r < nums.size(); r++) {
            if (nums[r] != val) {
                nums[l++] = nums[r];
            }
        }
        return l;
    }
};