I'm going to be unemployed Day 1 Sword Finger Offer 4. Find in a two-dimensional array

Affected by the epidemic, it can only be a home. The atmosphere without a laboratory has completely become a salted fish, and the small papers have not been restored to the teacher. Today, I began to record the process of brushing the sword and finger offer.
The topics are: 4. Search
for a two-dimensional array In an n * m two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.
Example: The
existing matrix is ​​as follows:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.

code show as below:

class Solution {
public:
    bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
        bool a = false;
        if (matrix.size() == 0)
        {return false;}
        int rows = matrix.size();
        int cols = matrix[0].size();
        
        
        int row = 0;
        int col = cols-1;
        while(row<rows && col>=0)
        {
            if (matrix[row][col] == target)
            {a = true;
            break;}
            else if (matrix[row][col] > target)
            col--;
            else row++;
        }
        
        return a;
           
    }
};

The idea is to compare the target number with the element in the upper right corner of the matrix in a loop, and return true directly if they are equal. There are two cases of inequality: (1) The element in the upper right corner is greater than the target, indicating that this entire column is larger than the target To delete this column, (2) The element in the upper right corner is less than the target, it means that an entire row of elements is less than the target, delete this row; the way to delete is achieved by row ++ and col–, rather than really operate on the matrix, as long as by increasing Or reduce the variables that control the loop, you can bypass the item you want to delete, just do not access it. If the deletion is complete or not, return false.
The more troublesome is the case of an empty array. The pointer is used for the pointer offer, so it is good to judge whether the matrix is ​​nullptr, but here is the array, and it cannot recognize null, just look at the method in the force deduction answer. If the size is 0 returns false, but there is still a problem, I set a boolean value a, if the empty array is a = false, but the compilation can not pass, I am crazy! ! ! Can anyone tell me why?
Then need to add knowledge points:
matrix.size () is the number of rows in the two-dimensional array, matrix [0] .size () is the number of columns.
Then it is here to "delete" the rows and columns by changing the variables that control the loop, excluding the rows or columns that need to be deleted from the loop body, and delete them in disguise, but the method in the upper right corner of this question is normal Who can come up with it ...
Continue to liver after nap!

Published 3 original articles · liked 0 · visits 16

Guess you like

Origin blog.csdn.net/qq_41227231/article/details/105658581