Finding in a two-dimensional array

Source: Internet
Author: User

Title Description
In a two-dimensional array, 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.

Thinking of solving problems

Iterates from the upper-right element, deleting the entire row if it is smaller than the target, or deleting the entire column if it is larger than the target. Each execution deletes a row or column and executes up to 2n times.

Implementation code
classSolution { Public:BOOLFind ( vector<vector<int> > Array,intTarget) {intrend =Array. Size ();intCend =Array[0].size (); for(inti =0, j = cend-1; I < rend && J >=0;) {if(ArrayI [j] = = target) {return true; }Else if(ArrayI            [j] < target) {i++; }Else{j--; }        }return false; }};

Finding in 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.