8

This topic aims to share some interesting or valuable topics found in the process of brushing Leecode. [Of course, the answer is based on js].

topic related

  • Original title address: https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/
  • Topic description:

    In an n*m 2D array, each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Please complete an efficient function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.
    现有matric如下:
    [
      [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]
    ]
    给定 target = 5,返回 true。
    给定 target = 20,返回 false。
    限制条件
     0 <= n <= 1000
     0 <= m <= 1000

Analysis of ideas

  • First of all, brute force cracking definitely the easiest to think of, but it is obviously wrong, if it is brute force cracking, the complexity of the extreme worst case is m*n > 1000000 (if the brute force can be used, the monotonically increasing condition of the question is meaningless, if the interview answer Use violence to break the crack, it is estimated that the interviewer is like this:)
    image.png

    Afterwards:

    image.png

  • Secondly, consider how to use monotonicity. If it is a one-dimensional array, it is easy to think of the dichotomy , but this is a two-dimensional array, and this idea obviously does not work.
  • That doesn't work, that doesn't work either. Then in the end, it can only be considered from the tree. Note that the rule written in the array is: each row increases from left to right, and each column increases from top to bottom, then it is obvious (easy to know (#^.^# )), assuming that regards the upper-right corner element as the root node of a tree, then based on it, the search to the left decreases in turn, and the downward search increases by , as follows:

    image.png

    Did you find anything?

    image.png

( new chicken times wow once touched hi many times !) This is a binary search tree ah! Of course, there may be students who don't know what a binary search tree is, so it's not a big problem. Simply put, the left child of each node must be smaller than its right child. then the solution is the paper!

Start the search from the upper right node (the lower left corner is also possible, for the same reason):

  • If target < current node, then means that the result can only fall on the subtree on the left, and the subtree on the right does not need to check , which corresponds to the two-dimensional array -- moves one column to the left ;
  • If target > current node, then means that the result can only fall on the subtree on the right, and the subtree on the left does not need to check , which corresponds to the two-dimensional array -- down a line of ;
  • If target = current node, then good! found, return true
    If the traversal of all nodes is still not found, then the target node does not exist, end;

full code

After understanding the previous core content, in fact, the code is not difficult to write, and finally paste the completed code:

/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var findNumberIn2DArray = function(matrix, target) {
    const m = matrix.length;
    if(m === 0 ) {return false};
    const n = matrix[0].length;

    for(let i = 0,j = n-1; i < m && j >= 0;) {
        if(target === matrix[i][j]){
            return true;
        }
        if(target > matrix[i][j]) {
            i++;
            continue;
        }
        if(target < matrix[i][j]) {
            j--;
        }
    }
    return false;
};

A simple question is done again!
image.png


安歌
7k 声望5.5k 粉丝

目前就职于Ringcentral厦门,随缘答题, 佛系写文章,欢迎私信探讨.