Algorithm deliberate practice -LeetCode combat -03 removing elements

Topic: removing elements

Topic links: (https://leetcode-cn.com/problems/remove-element/)
First impression:
Similar to the removal of the same element of yesterday
(see: https: //blog.csdn.net/DZZ18803835618/article / details / 104607900)
first, the idea of two-pointer brought on by:

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

But too slow:
Here Insert Picture Description
two tries:
read the official answer, try a little, really speed doubled times (probably because not many test cases need to remove the element of reason)
principle is probably this: that only in the case of to remove the element, the element is covered with the last bit of this element, the array length minus one, compared to the " first impression " to save time without removing a lot of copy elements, but requires a large amount of the removed elements test case, the time should be about the same. code show as below:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int n = nums.size();
        int i = 0;
        while(i < n)
        {
            if(nums[i] == val)
            {
                nums[i] = nums[n - 1];
                n--;
            }
            else
            {
                i++;
            }
        }
        return n;
    }
};

Here, I would like to say a few words while use of their habit may be the reason, every time the loop structure is used habitually used for, for use may be more sophisticated, but also a concrete analysis of specific scenes.
Here Insert Picture Description
Three attempts:
At this point, speed fairly plausible, unwittingly point to open the fastest solution use cases and found that you can use erase STL containers (), after finding this direct eliminate elements:

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

After writing and confidently submit the results:
Here Insert Picture Description
This unscientific ah, the same code, why I only beat 30% of people, With unyielding fighting spirit, and resubmit found that quality every time leap ah ~ ~
Here Insert Picture Description
it seems everyone in the future if they feel that their code has very sophisticated, but the time given unsatisfactory, you can try more than once (if you know that even if very violent)

Digression :
Before that, my not very fond of writing code, although it is not very fond of, but finally recognize his position. As a junior computer Coban consciousness now quite late, after the last sentence of each article is attached with it, depending on the mood may be, count on their own motivation, and the king of mutual encouragement.

Never early, never late

Published 16 original articles · won praise 0 · Views 286

Guess you like

Origin blog.csdn.net/DZZ18803835618/article/details/104625215