Find from a two-dimensional array (i)

Source: Internet
Author: User

In a two-dimensional array (each one-dimensional array is the same length), 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.

functionFind (target, array) {//you can find the corresponding element from the lower left corner of the two-dimensional array, if the target value    //is greater than the number of the lower left corner of the two-dimensional array, the target value is not in the first column    //if the target value is less than the lower-left corner of the two-D array, then the target value is not in the last row    varRownum=array.length-1; varColnum=array[0].length-1; //the number of the lower left corner of the two-dimensional array is represented as array[row-1][0]    varI=rownum,j=0; //need to have a loop     while(I>=0 && j<=colnum) {        if(target==Array[i][j]) {            return true; }        if(array[i][j]>target) {i--; //continue;//instructions to end this cycle, go to the next loop                    //description if ARRAY[I][J] will end this cycle if it is greater than target and then row--, it will have no effect on Col.}Else if(array[i][j]<target) {J++; //continue;        }    }    return false; }

Find from a two-dimensional array (i)

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.