Finding in a two-dimensional array-Sword point offer

Source: Internet
Author: User

Description of the lookup topic in a two-dimensional array

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.

Enter a description

Array: two-dimensional array to find

Target: The number found

Output description

Find to return True, find not to return false

Ideas
    1. Because the array is ascending from left to right from top to bottom, so we can start from the upper right corner to find, if the upper right corner of the number than target, then it is in the column is large, can be shed, until the small, and then start looking down, if the upper-right corner than target small, it is located in this column is small, You can drop it down until you find one that is greater than or equal to target.
    2. We can also start looking from the lower left corner, the truth is the same, is the size of the comparison to the reverse
Code
PublicClass Solution {public BooleanFind(int []Arrayint target) {Boolean found =Falseif (Array! = NULL &&Array.Length >0 &&array[0].length > 0) {int row = 0; int column = array[0].length- Span class= "Hljs-number" >1; while (Row <= (array.length-1 ) && column >= 0) {if ( Array[row][column] = = target) {found = true; break;} else if (array[row][column] > target) {column--;} else {row++;}}} return found;}            

Find in a two-dimensional array-Sword point offer

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.