Everyday a Leetcode Problem - 27. Remove Elements

Let’s move on to next day problem! I am doing Top Interview 150 if you wonder.

Here is the link for today’s problem: 27. Remove Elements.

Ideas

  • Use erase() and remove() functions to remove the elements equal to val.
  • Use sort() and copy constructor to make expectedNums;
  • Return like requirements.

Explanation

  • The remove() function rearranges the elements in the range [vec.begin(), vec.end()], removing all elements that are equal to val. It returns an iterator to the new end of the range.
  • The erase() function is then used to actually remove the elements from the vector, effectively resizing it.

Solution

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        nums.erase(remove(nums.begin(),nums.end(),val),nums.end());
        sort(nums.begin(), nums.end());
        vector<int> expectedNums(nums);
        return expectedNums.size();
    }
};

The runtime and the memory is hell but I am lazy to code longer…


See you next time!




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • Everyday a Leetcode Problem - 88. Merge Sorted Array
  • Everyday a Leetcode Problem - 26. Remove Duplicates from Sorted Array
  • Everyday a Leetcode Problem - 26. Remove Duplicates from Sorted Array
  • August 24 Reading Log
  • first month internship