Sword Point Offer (i) find a two-dimensional array

Source: Internet
Author: User

The title is described in a two-dimensional array in which each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom.  Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer. The first method, in each line of the two-point lookup, Nlogn. The second method, may also be the best, because it is already orderly, we choose the lower left or right corner, each time to determine the size, choose to move up or right, so every time there is greedy choice, but how to determine the complexity of the time? Pay special attention to the judgment of the boundary! Otherwise null pointer exception. Get the incoming array and get the subscript among other things to judge.
classSolution { Public BooleanFind (intTargetint[] Array) {        if(Array.Length = = 0)return false; //start from the lower left corner        intr = Array.length-1; intL = 0;  while(true) {            if(r<0 | | l>array[0].length-1)return false; if(Array[r][l] >target) {R--; }Else if(Array[r][l] <target) {L++; }Else {                return true; }        }    }} Public classMain { Public Static voidMain (string[] args) {intA = 16; int[] Array = {}; Solution So=Newsolution (); System.out.println (So. Find (16, array)); }}
classSolution { Public BooleanFind (intTargetint[] Array) {         for(inti = 0; i < Array.Length; i++) {            intL = 0; if(array.length<=0)return false; if(array[0].length<=0)return false; intr = Array[0].length-1;  while(l<=r) {intMid = (l+r)/2; if(target>Array[i][mid]) {L= Mid+1; }                Else if(target<Array[i][mid]) {R= Mid-1; }                Else                    return true; }        }        return false; }} Public classMain { Public Static voidMain (string[] args) {intA = 16; int[] Array = {{1,3,4},{3,16,1}}; Solution So=Newsolution (); System.out.println (So. Find (16, array)); }}

Sword Point Offer (i) find a two-dimensional array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.