[Algorithm beating diary] day01 - double pointer algorithm _ move zero, copy zero

 283. Moving Zero 

283. Move Zeroes icon-default.png?t=N6B9https://leetcode.cn/problems/move-zeroes/

topic:

Given an array  nums, write a function to  0 move all to the end of the array while maintaining the relative order of the nonzero elements.

Note  that arrays must be manipulated in-place without copying them.

 

 Problem-solving ideas:

We can use two pointers (dest and cur) to divide this array into three areas

We can initialize dest to -1 and cur to 0

 There are two situations encountered by cur walking through the array:

  • cur position is 0
  • The position of cur is not 0

When the position of cur is 0, cur++;

When cur is not 0, just swap it, dest++;

Problem-solving code:

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int dest=-1;
        int cur=0;
        int size=nums.size();
            while(cur<size)
            {
                if(nums[cur]!=0)
                {
                      swap(nums[dest+1],nums[cur]);
                    ++dest;
                }
                ++cur;
            }
    }
};

1089. Overwrite Zero

1089. Duplicate zeros icon-default.png?t=N6B9https://leetcode.cn/problems/duplicate-zeros/

 topic:

Given an array of integers of fixed length  arr , please overwrite every occurrence of zero in the array and shift the remaining elements to the right.

Note: Please do not write elements beyond the length of the array. Please do the above modification in-place on  the input array  and don't return anything from the function.

 Problem-solving ideas:

 This question requires us to modify it in place, and we cannot use additional arrays. Let's think about it with additional arrays first.

We found that the result is this

 Let's use the double pointer method using an additional array to experiment locally. We found that dest=-1 and cur=0, it is not possible to solve the problem forward, and elements will be overwritten and lost!

Let's try to operate in reverse, dest=size-1 (pointing to the last element), then how do we determine the position of cur?

We can use the double pointer method again to confirm the position of cur. At the beginning, cur=0, dest=-1

Traverse the array, when cur encounters 0, dest+=2; when cur encounters non-zero, cur++;

There will be a boundary case here that we need to consider:

Example: 1 0 2 3 0

When we determine cur through the above method, the following situation will occur:

There will be a situation where dest is out of bounds. We will set the position of size-1 to 0, and then cur--, dest-=2 will solve the problem.

Then when you encounter 0 from the back to the front, you will set 0 twice, and if you encounter non-zero, you will copy it. This step is simple

 

 Problem-solving code:

class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int dest=-1;
        int cur=0;
        int size=arr.size();
        //确认cur的位置
        while(cur<size)
        {
            if(arr[cur]==0)dest+=2;
            else dest++;

            if(dest>=size-1)break;
            cur++;
        }
        //边界情况,示例:1 0 2 3 0,当dest的位置越界了
        if(dest==size)
        {
            arr[size-1]=0;
            dest-=2;
            cur--;
        }
        while(cur>=0)
        {
            if(arr[cur]!=0)
            {
                arr[dest--]=arr[cur--];
            }
            else 
            {
                arr[dest--]=0;
                arr[dest--]=0;
                cur--;
            }
        }
    }
};

 

Guess you like

Origin blog.csdn.net/m0_69061857/article/details/132231760