Find (dichotomous thinking) a two-dimensional array

Description Title
(same as the length of each one-dimensional array) in a two-dimensional array, each row from left to right in order of ascending sort, to sort each column from top to bottom in increasing order. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

public class Solution {
    public boolean Find(int target, int [][] array) {
        boolean flag=false;
        int n=array.length;//行数
        int m=array[0].length;//列数
        int row=n-1,col=0;
        while(row>=0&&col<m){
            int x=array[row][col];
            if(target>x) col++;
            else if(target<x) row--;
            else {
                flag=true;
                break;
            }
        }
        return flag;
    }
}
Published 428 original articles · won praise 55 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42936517/article/details/105180102