[Jianzhi offer] 04. Search in two-dimensional array

Warm Reminder/Opening Introduction/Speaking Before Brushing Up

Starting today, I started to write questions, I believe this is a long process!

This column is the classic topic of "Jianzhi offer",

And record your understanding and learning process in this blog !

Topic introduction (LINK)

As for the topic, I won’t introduce too much, it’s not very meaningful to copy it, so I’ll just post the link here!

LeetCode link: click here!

NowCoder link: click here!

own thoughts/thoughts

1. Initial Thoughts / Final Thoughts

In fact, my idea is to search while traversing the two-dimensional array , which is relatively simple and simple!

However, in fact, what the interviewer wants to see is not this ,

Because anyone can think of such a simple logic!

The feature of the topic is not even used: sorting in increasing order!

A good idea is this:

  • The essence of the search process is the process of exclusion. It is best if you can exclude a batch!

Look at the picture below:

insert image description here

Finally, there is a critical question to consider : when will it end?

In the end, if it is not found, it means that the condition of a row or column out of bounds leads to dissatisfaction!

2. Notes

If you see the word "whether or not " in the title , you can judge that the return value is a Boolean type!

Code for my first attempt

I didn't write it out at the beginning, so I got confused!

Then I wrote it after a simple understanding (this is the so-called violent law, which is meaningless):

public class Solution {
    
    
    public boolean Find(int target, int [][] array) {
    
    
        //数组为空或者长度为0直接返回false
        if (array == null || array.length == 0) {
    
    
            return false;
        }
        
        for (int i = 0; i < array.length; i++) {
    
    
            for (int j = 0; j < array[0].length; j++) {
    
    
                if (array[i][j] == target) {
    
    
                    return true;
                }
            }
        }
        return false;
    }
}

code after research

class Solution {
    
    
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
    
    
        //如果为空或者长度为0,直接返回false
        if (matrix == null || matrix.length == 0) {
    
    
            return false;
        }

        int i = 0;//列下标
        int j = matrix[0].length - 1;//行下标,第一行的长度-1

        while (i < matrix.length && j >= 0) {
    
     //这个就是整个查找的范围
        //范围就是:

            if (target < matrix[i][j]) {
    
    
                j--;//排除列
            }else if (target > matrix[i][j]) {
    
    
                i++;//排除行
            } else {
    
    
                return true;
            }

        }
        return false;
    }
}

作者:cbiltps
链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/solution/yong-you-shang-jiao-de-zhi-bi-jiao-ji-ke-gz3e/
来源:力扣(LeetCode
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Thank you

When the first question comes to the question, I just pull the switch, I didn’t make it, and I just read the answer...

a bit lost...

Guess you like

Origin blog.csdn.net/Cbiltps/article/details/122757705