Sword refers to the offer series-in a two-dimensional array, each row increments from left to right, and each column increments from top to bottom. design an algorithm to find a number.

Source: Internet
Author: User

Sword refers to the offer series-in a two-dimensional array, each row increments from left to right, and each column increments from top to bottom. design an algorithm to find a number.

Question:In a two-dimensional array, each row increments from left to right, and each column increments from top to bottom. design an algorithm to find a number

Analysis:

The two-dimensional array is regarded as a rectangle structure ,:

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

When doing this, I first considered that the elements on the diagonal line may achieve better results each time,

Take search 9 as an example,

Starting from 1 () and 1 <10, we can conclude that 10 is on the right or bottom of 1;

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

 

Then we can see 4 (), 4 <9,

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

Then check 10 (), 10> 9. At this time, you can determine that the value is in the upper right or lower left corner of 10,

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

Then we can consider using recursive yellow boxes. Set an> x to (x, y), an-k> x to (x0, y0 ), the starting point of this recursion is: upper right (x, y0), lower left (x0, y ).

Box in the lower left, 4 (), 4 <9; then look at 8 (), 8 <10, you can confirm that the box is not in the lower left;

In the upper right corner of the box, 8 (), 8 <9; then 12 (), 12> 9, recursive again, () or () can be found.

 

It seems that the problem can be solved, but the rectangle must be a square at this time ......

       
       
       
       
       
       
       
       

 

As shown in, there is no way (of course, you can use the relationship between width and height to calculate the number of vertical moves and the number of horizontal moves, which can take exactly the diagonal corner. However, it's too troublesome ....), Therefore, this is not true ......

 

Then we can consider that we can remove one row or one column after comparison. This method can be considered as good.

Take Search 10 as an example,

1. If you look for it from the upper left corner, 1 <10, it is found that there is no better way than one after another;

2. If you start searching from the upper right corner (), 9 <10, since each row increments from left to right, You can exclude the first line (0;

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

Next, let's look at the last number (1, 3), 12> 10 in the second row. Since each column increments from top to bottom, we can exclude the last column (3;

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

Then check (1, 2), 9 <10, and exclude line 1;

1

2

8

9

2

4

9

12

4

7

10

13

6

8

11

15

 

Then look at (), 10 = 10, find the target, and return the row and column number.

3. in addition to better analysis results from the upper right corner, we can find that one row or one column can be excluded by comparison at a time in the lower left corner. The analysis from the lower right corner is the same as that from the lower right corner, and the efficiency is not high.

Code:

Package study;/*** in a two-dimensional array, each row increments from left to right, and each column increments from top to bottom, * design an algorithm to find a number * @ author cnx * @ CreateDate; September 28, 2014 10:14:21 */public class FindInMatrix {public static void main (String [] args) {FindInMatrix f = new FindInMatrix (); int a [] = f. find (f. a1, 12); if (! = Null) System. out. println ("a1 find 12. row = "+ a [0] +"; col = "+ a [1]); a = f. find (f. a2, 18); System. out. println ("a2 find 18. row = "+ a [0] +"; col = "+ a [1]); a = f. find (f. a2, 2); System. out. println ("a2 find 2. row = "+ a [0] +"; col = "+ a [1]); a = f. find (f. a2, 7); System. out. println ("a2 find 7. row = "+ a [0] +"; col = "+ a [1]);} public int a1 [] [] = null; public int a2 [] [] = {,}, {,}, {, 11, 15 }}; // search for, 7 public int [] find (int a [] [], int des) {// Number of stored rows and columns int result [] = {-1,-1}; if (! = Null &. length> 0 & a [0]. length> 0) {int row = 0; int col = a [0]. length-1; while (row <. length & col> = 0) {// System. out. println (. length + "," + a [0]. length); if (a [row] [col] = des) {result [0] = row; result [1] = col; break ;} if (a [row] [col]> des) {col --;} else {row ++ ;}} else result = null; return result ;}}

 


Design a program to put the maximum element of each row in a two-dimensional array and the minimum element of each column into two one-dimensional arrays respectively.

Do you want to ask the Train of Thought? a [m] [n] array, m control row, n control column, for example, to find the minimum column, use a for statement to control the number of columns cyclically, compare a [m] [constant controlled in for], place the smallest one in a comparison interchange statement, and the result is to import a [0] [n] to the new array.

Given two-dimensional arrays A and B, the elements are arranged in ascending order and A program is compiled.

Int a [5] = {4, 5, 6, 11, 12 };
Int B [3] = {1, 2, 8 };

Int c [8];

For (int I = 0, j = 0, k = 0; 5! = J | 3! = K; ++ I)
{
If (5 = j)
{
C [I] = B [k];
++ K;
Continue;
}
If (3 = k)
{
C [I] = a [j];
++ J;
Continue;
}
If (a [j] <= B [k])
{
C [I] = a [j];
++ J;
}
Else
{
C [I] = B [k];
++ K;
}
}

For (int I = 0; I <8; ++ I)
{
Cout <c [I] <endl;
}

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.