Hello everyone, I am quick-frozen fish 🐟, a river front 💦, like the garish 💐, continuous sand sculpture 🌲 Welcome friends to add my wechat: Sudongyuer pull you into the group, discuss together, looking forward to growing together with everyone

Preface 🌧 ️

Algorithms are unfamiliar and familiar to the front-end people, and often we don’t value them as much as the back-end engineers do. But in fact, algorithms have an unshakable position for every programmer.

Because the development process is to convert the actual problem into the computer can recognize the instructions, that is, “data structure” said, “design the data structure, in the application of algorithms on the line”.

In today’s environment, algorithms have become an indispensable skill for front-end engineers. If we want to move beyond being application engineers writing business code, we need to understand algorithms and data structures.

Of course, learning is also focused, as the front end we do not need to fully grasp the algorithm like back-end development, some of the more partial, not practical type and solution method, as long as a little understanding.

The title 🦀

Reference to Offer 04. Search in a two-dimensional array

The difficulty of medium

In an N by m two-dimensional array, each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Perform an efficient function that takes such a two-dimensional array and an integer and determines whether the integer is in the array.

Example:

The existing matrix 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]]Copy the code

Given target = 5, return true.

Given target = 20, return false.

Limitations:

0 <= n <= 1000
0 <= m <= 1000
Copy the code

🌵

  • Break down big problems into smaller ones.
  • Start with the element in the upper right corner each time
  • If the selected element is less than the target element, the number of columns is reduced
  • If the selected element is larger than the target element, the number of rows is increased
  • If the project then exits found

Source 🔥

/ * * *@param {number[][]} matrix
 * @param {number} target
 * @return {boolean}* /
var findNumberIn2DArray = function(matrix, target) {
        if(! matrix||! target){return false
        }
        let row = 0
        let column = matrix[0].length-1
        while(row<=(matrix.length-1)&&column>=0) {console.log(row,column)
            const num=matrix[row][column]
             if(num===target){
                 return true
             }else if(num<target){
                   ++row 
             } else if(num>target){
                 --column
             }
        }
        return false
};
Copy the code

Time complexity: length and width of O(m*n) matrix

Space complexity :O(1)

Conclusion 🌞

So the “LeetCode” of The LeetCode of the algorithm chapter refers to the search in the two-dimensional array of offer-04 ⚡️. There is no shortcut to the algorithm, so we can only write and practice more and summarize more. The purpose of the article is actually very simple, which is to urge myself to complete the algorithm practice and summarize and output. I hope everyone can like my essay, and I also hope to know more like-minded friends through the article. If you also like to toss, welcome to add my friend, sand sculpture together, together progress.

Making 🤖 : sudongyu

Personal blog 👨💻: Frozen fish blog

Vx 👦 : sudongyuer

Write in the last

Guys, if you like my words, give 🐟🐟 a thumbs up 👍 or follow ➕ to support me the most.

Add me on wechat: Sudongyuer, invite you to join the group, learn front end together, become a better engineer ~ (group QR code here -> front end to go to bed early, qr code expired to see the link in the boiling point of the comments, I will put the latest QR code in the comment section, of course, you can also add my wechat I pull you into the group, after all, I am also interesting front end, Know me also not bad 🌟 ~ PS: the company recently in the recruitment front, welcome small friends to consult, coordinates chengdu, my VX: Sudongyuer, or direct delivery mailbox [[email protected]