To prove safety OFFER ---- 4, a two-dimensional array to find (js achieve)

topic

(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.


Thinking

By comparing the number of upper right corner


function Find(target, array)
{
    // write code here
    if (!target || !array || array.length === 0) {
        return false
    }
    let row = 0
    let column = array[0].length - 1
    while (row < array.length && column >= 0) {
        let rightTop = array[row][column]
        if (target === rightTop) {
            return true
        } else if (target < rightTop) {
            column--
        } else {
            row++
        }
    }
    return false
}

Guess you like

Origin blog.csdn.net/qq_40816360/article/details/95002825