头图

Zero title: Algorithm (leetcode, with mind map + all solutions) (240) of 300 questions to search for two-dimensional matrix II

a topic description

题目描述
题目描述
题目描述

Two solutions overview (mind map)

思维导图

All three solutions

Interviewer: The topic is almost the same, how is it? Do you have any ideas?

Madman Zhang San: When it comes to "mapping, quantity, repetition (ie, deduplication), uniqueness (ie, number of times), etc.", I may give priority to hash (corresponding to the map data structure in JS).

Interviewer: Then you can talk about the general idea first.

Madman Zhang San:

 1)遍历二维数组里的每个元素,将它们放在哈希表里。
2)最后判断 target 值,是否在哈希表里即可~


Interviewer: Hmm, the overall idea is ok, so let’s start writing

Madman Zhang San: After 2 minutes and 52 seconds, Zhang San finished writing the program

1 Scenario 1

1) Code:

 // 方案1 “自己。哈希法”。
// 注:通过 124 / 129,会超时。
// 用时:15:14 - 15:16。
// 技巧:涉及“映射、数量、重复性(即去重)、唯一性(即次数)等”可优先考虑hash(JS里对应的是 map数据结构)。

// 思路:
// 1)状态初始化:m = matrix.length, n = matrix[0].length;
// resMap = new Map();
// 2)遍历二维数组,将每个元素放入 resMap 中。
// 3)返回结果: resMap.has(target) 。
var searchMatrix = function(matrix, target) {
    // 1)状态初始化:m = matrix.length, n = matrix[0].length;
    // resMap = new Map();
    const m = matrix.length,
        n = matrix[0].length;
    let resMap = new Map();

    // 2)遍历二维数组,将每个元素放入 resMap 中。
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            const tempVal = matrix[i][j];
            resMap.set(tempVal, 1);
        }
    }

    // 3)返回结果: resMap.has(target) 。
    return resMap.has(target);
};

Next to: The code is running in the background. After 3 seconds, the screen suddenly [flashed] out [Pass 124/129, time limit exceeded].

Interviewer (with a hint of contempt on his face): Young man, when writing code, you need to talk about martial arts and make good use of the conditions of the question.

Madman Zhang San (after 60 seconds of reading the question carefully): Mmmm, in order, emmmmm ---> Is it possible to use binary search?

Interviewer: Think about it carefully

Madman Zhang San (after thinking about various ideas and templates of binary search): The overall idea is as follows:

 1)遍历二维数组的每一行,对每一行(即有序数组)使用二分查找 —— 判断 target 是否在里面。
2)若 每一行都不存在 target 值,则 返回 false ,否则返回 true 。


Interviewer:


Narrator: Within a few minutes, Zhang San wrote the code for the "binary search" method and passed all the cases.

2 Option 2

1) Code:

 // 方案2 “自己。二分查找”。
// 用时:15:37 - 15:43。
// 技巧:在有序的数组里进行元素的查找,可优先考虑“二分查找”。

// 思路:
// 1)状态初始化:m = matrix.length, n = matrix[0].length;
// resFlag = false; 。
// 2)核心:遍历二维数组的每一行,对每一行(即有序数组)使用二分查找 —— 判断 target 是否在里面。
// 2.1)若 target 在当前行 —— isFindTarget(matrix[i], target),
// 则 resFlag = true 并退出搜索。
// 3)返回结果: resFlag 。
var searchMatrix = function(matrix, target) {
    const isFindTarget = (list = [], target = 0) => {
        const l = list.length;
        let left = 0,
            right = l - 1,
            resFlag = false;

        while (left <= right) {
            const mid = left + Math.floor((right - left) / 2),
                midVal = list[mid];

            if (midVal === target) {
                resFlag = true;
                break;
            }
            else if (midVal > target) {
                right = mid - 1;
            }
            else {
                left = mid + 1;
            }
        }

        return resFlag;
    };

    // 1)状态初始化:m = matrix.length, n = matrix[0].length;
    // resFlag = false; 。
    const m = matrix.length,
        n = matrix[0].length;
    let resFlag = false;

    // 2)核心:遍历二维数组的每一行,对每一行(即有序数组)使用二分查找 —— 判断 target 是否在里面。
    for (let i = 0; i < m; i++) {
        // 2.1)若 target 在当前行 —— isFindTarget(matrix[i], target),
        // 则 resFlag = true 并退出搜索。
        if (isFindTarget(matrix[i], target)) {
            resFlag = true;
            break;
        }
    }

    // 3)返回结果: resFlag 。
    return resFlag;
}

Interviewer: Look carefully at the stem of the question - "The elements in each row are in ascending order from left to right. The elements in each column are in ascending order from top to bottom." So do we start the search from some special position?

Madman Zhang San (looks carefully): Hmm, it seems that the lower left corner and upper right corner are the middle elements, we can start the search from them.

Interviewer:


Madman Zhang San:

 比如,
1)我们从左下角开始不断【向上、向右】搜索。
1.1)若 当前值 tempVal === target,则 退出搜索。
1.2)若 当前值 tempVal < target,则 向右搜索。
1.3)若 当前值 tempVal > target,则 向上搜索。


Interviewer:


3 Scenario 3

1) Code:

 // 方案3 “自己。Z 字形查找”。
// 用时:15:22 - 15:27。

// 想法:
// 1)题干中,“每行的元素从左到右升序排列。每列的元素从上到下升序排列。”。
// 2)那么我们开始选择较为中间的位置开始搜索,若当前值偏小就向右搜索、偏大就向上搜索、相等就退出搜索。

// 思路:
// 1)状态初始化:m = matrix.length, n = matrix[0].length;
// i = m - 1, j = 0【从左下角开始搜索】, resFlag = true;

// 2)核心:从左下角开始不断【向上、向右】搜索。
// 2.1)若 当前值 tempVal === target,则 退出搜索。
// 2.2)若 当前值 tempVal < target,则 向右搜索。
// 2.2)若 当前值 tempVal > target,则 向上搜索。

// 3)返回结果 resFlag 。
var searchMatrix = function(matrix, target) {
    // 1)状态初始化:m = matrix.length, n = matrix[0].length;
    // i = m - 1, j = 0【从左下角开始搜索】, resFlag = true;
    const m = matrix.length,
        n = matrix[0].length;
    let i = m - 1,
        j = 0,
        resFlag = true;

    // 2)核心:从左下角开始不断【向上、向右】搜索。
    while (true) {
        if (i < 0 || i >= m || j < 0 || j >= n) {
            resFlag = false;
            break;
        }

        const tempVal = matrix[i][j];
        // 2.1)若 当前值 tempVal === target,则 退出搜索。
        if (tempVal === target) {
            break;
        }
        // 2.2)若 当前值 tempVal < target,则 向右搜索。
        else if (tempVal < target) {
            j++;
        }
        // 2.2)若 当前值 tempVal > target,则 向上搜索。
        else {
            i--;
        }
    }

    // 3)返回结果 resFlag 。
    return resFlag;
};

Narrator: After Zhang San wrote the above code, he hurriedly asked the interviewer.

Madman Zhang San: Did you pass the interview?

Interviewer:


Madman Zhang San:

Another offer, and I'm going to be the CEO soon. Should I go to eat Shaxian snacks in the evening? Or Lanzhou Ramen ? Alas, having too many choices is also an annoyance!

Four resource sharing & more

1 Historical Articles - Overview

历史文章 - 总览
历史文章 - 总览

刷题进度 - LeetCode:578 / 2722 、《剑指offer》:66 / 66

2 Introduction to bloggers

Code Farmer Sanshao, a blogger dedicated to writing minimalist but complete problem solutions (algorithms ).
Focus on one question, multiple solutions, structured thinking , welcome to brush through LeetCode ~


码农三少
54 声望8 粉丝