Finding in a two-dimensional array of "sword point offer surface"

Source: Internet
Author: User

Make a decision to ac all the swords point to the offer surface question.
Nine degree OJ face question address: http://ac.jobdu.com/hhtproblems.php
Book: Huang--"Jian refers to offer: Famous enterprise interviewer explaining typical programming problem"
for interview questions, interviewers often prefer that we can propose optimization methods, which can reflect our thinking ability as well as the legendary "internal strength". So the sword refers to offer to focus on training this aspect, more summed up more scrutiny, always have the advantage. Cheer Up ~

finding in a two-dimensional array

Time limit: 1 seconds memory limit: 32 trillion special problem: No submit: 19005 Resolution: 3642
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.
Input:
The input may contain multiple test samples, for each test case,
The first behavior of the input two integers m and n (1<=m,n<=1000): Represents the number of rows and columns of the matrix that will be entered.
The second line of input includes an integer t (1<=t<=1000000): Represents the number to find.
The next M-line, each row has n number, represents the title of the M-row N-column matrix (The matrix as shown in the title description, each row is ordered from left to right increments, each column is sorted from top to bottom in order.
Output:
corresponding to each test case,
The output "Yes" indicates that a number T is found in a two-dimensional array.
The output "No" indicates that no number T is found in the two-dimensional array.
Sample Input:
7 ·
5
1 2 3
4 5 6
7 8 9
7 ·
1
2 3 4
5 6 7
8 9 10
7 ·
12
2 3 4
5 6 7
8 9 10
Sample output:
Yes
No
No

On the nine-degree OJ, submit the question 3 on the offer by means of the sword.
when we need to solve a complex problem, a very effective way is to start with a specific problem, through the analysis of simple concrete examples, trying to find common laws.

rule:
First, select the number in the upper-right corner of the array. If the number equals the number you are looking for, the lookup process ends with true, and if the number is greater than the number you are looking for, the element of the column must be greater than the number you are looking for, excluding the column where the number is located, and if the number is less than the number you are looking for, exclude the row that contains the number. That is, if the number you are looking for is not in the upper-right corner of the array, each time you exclude a row or column from the array's look-up, each step narrows the search until you find the number you are looking for, or the lookup range is empty to return Fasle.

C + + implementation:

/*********************************-----------------------------------"sword refers to the offer surface question" Find in a two-dimensional array-----------------------------------Author: date:2015 email:[email protected] ************************* *********/#include <stdio.h> #include <iostream> using namespace std;/ * Find if there is a number in the matrix of two-dimensional array * /BOOL Find (int* Matrix,intRowsintColumnsintNumber) {bool found =false;if(Matrix! = NULL && rows >0&& columns >0)    {introw =0;intcolumn = Columns-1; while(Row < rows && column >=0)        {if(Matrix[row * columns + column] = = number) {found =true; Break; }Else if(Matrix[row * columns + column] > number)--column;Else+ + row; }    }returnFound;}intMain () {intM,n; while(SCANF ("%d%d", &m,&n)! = EOF) {intT,i;intmatrix[ +* +] = {0}; scanf"%d", &t); for(i=0; i<m*n;i++) scanf ("%d", matrix+i); BOOL result = Find (matrix,m,n,t);if(result) printf ("yes\n");Elseprintf"no\n"); }return 0; }/************************************************************** problem:1384 language:c++ result:accepted time:850 Ms memory:5356 kb****************************************************************/

This can only be used scanf printf input output, with CIN cout test for time Limit exceed not pass.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Finding in a two-dimensional array of "sword point offer surface"

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.