To prove safety Offer_ programming problem _ a two-dimensional array to find

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/JY_WD/article/details/102242405

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.

AC Code

public class Solution {
    public boolean Find(int target, int [][] array) {
        for(int i=0;i<array.length;i++){
            int low=0;
            int high=array[0].length-1;
            while(low<=high){
                int mid=(low+high)/2;
                if(target>array[i][mid]){
                    low=mid+1;
                }else if(target<array[i][mid]){
                    high=mid-1;
                }else{
                    return true;
                }
            }
        }
            return false;
    }
}

Topic analysis

Find a special two-dimensional one-dimensional thinking owned by the same route, use a binary search on it

Guess you like

Origin blog.csdn.net/JY_WD/article/details/102242405