1 Series offer to prove safety

Foreword

Recently looking at the cattle off network interview questions, a lot of questions to prove safety offer found inside the online Zenmo not easy to find the answer, then I make a note of it, but also right when providing a reference for colleagues, because it is the first time write, so bad places also hope forgive me.

Title Description

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.
Personal feeling es6 use an array of methods some queries it may be more simple, the actual running time: 142ms memory for: 11028k
function Find(target, array)
{
   return array.some(item => {
        return item.some(items => {return items === target}) === true
    })
    
}
module.exports = {
    Find : Find
};

User method

The basic principle is similar to the first query and then compare the size of the specific line

function Find(target, array) {
            
    let i = 0
    let j = array[i].length - 1
    let min = array[0][0]
    let max = array[array.length-1][array[0].length-1]
 
    if (target < min || target > max) 
        return false
                    
    while (i < array.length && j >= 0) {
        if (array[i][j] < target) {
            i++
        } else if (array[i][j] > target) {
            j--
        } else {
            return true
        }
    }
    return false
}

 

Guess you like

Origin www.cnblogs.com/xiaochensun/p/11582920.html