Their algorithm is not good, brush some algorithm questions, collation for their own review use ~

Implement binary search

function erFen(target,array) {
    let left = 0;
    let right = array.length - 1;
    while(left <= right) {
        let middle = Math.floor((left + right)/2);
        if (array[middle] === target) {
            return true;
        }else if (array[middle] < target) {
            left = middle + 1;
        }else {
            right = middle - 1; }}return false;
}
Copy the code

Perform the following

Find a two-dimensional array

In a two-dimensional array (each one-dimensional array is the same length), each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function that takes such a two-dimensional array and an integer and determines whether the integer is in the array.

Train of thought

  • First of all, there’s order whether it’s rows or columns
  • Compare target to the last bit of the first array or the first bit of the last array in a two-dimensional array

There will be a unique path, otherwise the path will be repeated

function Find(target, array){
    let col = 0;
    let row = array.length -1;
    while(row >= 0 && col <= array.length -1) {
        if (target < array[col][row]){
            row --;
        }else if(target === array[col][row]) {
            return true;
        }else{ col ++; }}return false
}
Copy the code