Likou 832. Flip the image-C language implementation-simple questions

topic

Portal

text

Given a binary matrix A, we want to flip the image horizontally first, then invert the image and return the result.
To flip a picture horizontally is to flip each line of the picture, that is, reverse the order. For example, the result of horizontally flipping [1, 1, 0] is [0, 1, 1].
Inverting the picture means that all 0s in the picture are replaced by 1s, and all 1s are replaced by 0s. For example, the result of reversing [0, 1, 1] is [1, 0, 0].

Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1 ,1]]
Explanation: First flip each line: [[0,1,1],[1,0,1],[0,0,0]];
then reverse the picture: [[1,0,0] ,[0,1,0],[1,1,1]]

Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1 ,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First flip each line: [[0,0,1 ,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]];
then reverse the picture: [[1,1,0,0] ,[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Description:

1 <= A.length = A[0].length <= 20
0 <= A[i][j] <= 1

Source: LeetCode.

template

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 *返回一个数组的大小。
 *数组的大小以*returnColumnSizes数组的形式返回。
 *注意:返回的数组和*columnSizes数组必须是malloced(),假设调用方调用free()。
 */
int** flipAndInvertImage(int** A, int ASize, int* AColSize, int* returnSize, int** returnColumnSizes){
    
    

}

Problem solving

analysis

Let’s take a look at the following two arrays. Insert picture description here
If you cancel the intermediate state, you will find that this transformation has a rule, which is based on the symmetry of the intermediate position. If the elements at the front and rear symmetric positions are the same, then the elements at the two positions after the transformation They will be different from the original, but if they are not the same, they will still have the same elements after the transformation.
The following figure uses'-' to indicate the meaning of non
Insert picture description here
Therefore, if you want to achieve the topic requirements, you can directly make a palindrome judgment and then directly determine whether the elements need to be changed.
First of all, it is natural to initialize

    *returnSize = ASize;
    returnColumnSizes[0] = AColSize; 

Then make a judgment, and directly fill in the decision after judgment

    for (int r = 0; r < ASize; r++) 
        for (int c = 0; c < (AColSize[r] + 1) / 2; c++)     
            if (A[r][c] == A[r][AColSize[r] - 1 - c]) 
                A[r][c] = A[r][AColSize[r] - 1 - c] = 1 - A[r][c]; 

You can return at the end, it's very simple.

Specific source code

int** flipAndInvertImage(int** A, int ASize, int* AColSize, int* returnSize, int** returnColumnSizes){
    
    
    *returnSize = ASize;
    returnColumnSizes[0] = AColSize; 
    for (int r = 0; r < ASize; r++) 
        for (int c = 0; c < (AColSize[r] + 1) / 2; c++)     
            if (A[r][c] == A[r][AColSize[r] - 1 - c]) 
                A[r][c] = A[r][AColSize[r] - 1 - c] = 1 - A[r][c]; 
    return A;
}

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44922487/article/details/114006889