Assignment 1.3 delete elements

Assignment 1.3

Remove element

Given an array of nums and a value of val, you need to remove all elements with a value equal to val in situ and return the new length of the array after the removal.
Don't use extra array space, you must modify the input array in place and do it with O (1) extra space.
The order of elements can be changed. You don't need to consider the elements in the array beyond the new length.
Insert picture description here

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
int i = 0;
        for (int j = 0; j < nums.size(); ++j)
        {
            if (nums.at(j) != val)
            {
                nums.at(i) = nums.at(j);
                ++i;
            }
        }
	    return i;
    }
   
};
Published 22 original articles · Likes0 · Visits 305

Guess you like

Origin blog.csdn.net/qq_45950904/article/details/104453584