Solution to Find an Integer in 2D Array from Jobdu

30 Jul

Question (in Chinese): http://ac.jobdu.com/problem.php?pid=1384

Question Name: Find an Integer in 2D Array

Question Description: in a 2D array, independently every row/column is already sorted in ascending order. Given a such 2D array and an integer, write a function to check whether the array contains that integer.

Input: there may be multiple test cases per execution. In each test case, the first line contains two integers m and n (1<=m,n<=1000), to indicate the number of rows and columns in the array respectively. The second line is an integer to be found in the array. The following m lines contain n integers in each, as the content of the matrix.

Output: print “Yes” if the matrix contains the integer, or “No” otherwise.

To solve it, we could use a method, simiar with binary search.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!