Leetcode array tag Title (in update)

Time:2019-9-11

905. Sort Array By Parity(easy)

Leetcode array tag Title (in update)
Topic Understanding:
All of the arrays are non-negative numbers. Now let’s put even numbers before and odd numbers after even numbers.

First thought: declare a new vector, scan A twice, insert an even number in the first pass, insert an odd number in the second pass, and return a new vector.
Possible optimization directions:

  • Scan it again
  • No allocation of new space

So I thought of a new method: marking the position with two pointers, two pointers scanning from the back to the back and from the back to the front respectively.
Comparing the data pointed by two pointers has four situations:

Situation Corresponding operation
Former odd even later Exchange position, I move backward, J move forward
Ex post odd No swap, I moves backward, J moves forward
The front and back are all even numbers No swap, I move backwards
The front and back are all odd numbers Instead of swapping, J moves forward

In fact, the essence of this method is to use two pointers in O (N) time to achieve the same effect as the previous two scans, and maintain the original array, no longer need to allocate new space, which is also a relatively common practice.
In the actual code, I made some changes to the timing of i, J movement. The result is the same, but it may seem simpler, and it can be divided into four cases, one by one, if… else if.
Accept code

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        int i = 0, j = A.size() - 1;
        int n = A.size();
        while (i < j)
        {
            if (A[i] % 2 != 0 && A[j] % 2 != 1)
                swap(A[i++], A[j--]);
            if (A[i] % 2 == 0)
                i++;
            if (A[j] % 2 == 1)
                j--;
        }
        return A;
    }

};

A key:

  • Two pointer

832. Flipping an Image

subject

Leetcode array tag Title (in update)
Understand:
The title has been made very clear, that is, to have a two-dimensional array containing only 0 and 1, first to reverse the order of each subarray, and then to turn 0 into 1, 1 into 0.

solution
Just follow the title.
Possible optimization:

  • Bit Operations to Convert 0,1

Accept code

class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        int size_A = A.size();
        for (int i = 0; i < size_A; i++)
        {
            int size_of_each_vector = A[i].size();
            int p_begin = 0, p_end = size_of_each_vector - 1;
            while (p_begin < p_end)
            {
                A[i][p_begin] ^= 1;
                A[i][p_end] ^= 1;
                swap(A[i][p_begin++], A[i][p_end--]);
            }
            if (p_begin == p_end)
                A[i][p_begin] ^= 1;
        }
        return A;
    }
};

Here I do the flip manually, and of course I can use the reverse function as well.

reverse(vec.begin(),vec.end());

By the way, when the number of elements in a subarray is odd, the middle number needs to be judged.
After a look at the official solution, I feel very concise. Learn (java version):

class Solution {
    public int[][] flipAndInvertImage(int[][] A) {
        int C = A[0].length;
        for (int[] row: A)
            for (int i = 0; i < (C + 1) / 2; ++i) {
                int tmp = row[i] ^ 1;
                row[i] = row[C - 1 - i] ^ 1;
                row[C - 1 - i] = tmp;
            }

        return A;
    }
}



Recommended Today

Resolving Time Zone Errors with Docker

1. The correct time zone in China is set to CST, i.e.China Standard TimeIn usedockerstart-upJenkinsWhen mirroring, the settings for mirroring are usuallyCoordinated Universal Time。 So add parameters at startup -v /etc/localtime:/etc/localtimeMount the local time zone to the mirror, so that the mirror gets the correct time zone. Then go to Jenkins and fill in the […]