Find a two-dimensional array of Java implementation [to prove safety offer]

topic

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

To find a rectangular 1

1.1 Description

Videos in a rectangular two-dimensional array, and then select a number from the array, in the case of 3 points to find the analysis, the time complexity o (n * m), the spatial complexity o (1)

1.2code

 1 public class ArrayFind {
 2     private static int[][] A;
 3  
 4     private static int[][] initArray() {
 5         int[][] A = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 },
 6                 { 6, 8, 11, 15 } };
 7         return A;
 8     }
 9  
10     private static void find(int target) {
11         boolean found=false;
12         if (A != null) {
13             int row =0;
14             int column = A[0].length-1;
15             int key;
16             while (row < A.length && column >= 0) {
17                 key = A[row][column];
18                 System.out.println("now key="+key);
19                 if (key == target) {
20                     found=true;
21                     break;
22                 } else if (key < target) {
23                     ++row;
24                 } else {
25                     --column;
26                 }
27             }
28             if(found){
29                 System.out.println("find the targe "+target+" in :" + row + ","
30                         + column + "!");
31             }
32             else{
33                 System.out.println("Sorry not found!");
34             }
35         }
36  
37     }
38 is   
39      public  static  void main (String [] args) {
 40          // the TODO Auto-Generated Method Stub 
41 is          A = initArray ();
 42 is          Find (14 );
 43 is          
44 is   
45      }
 46 is   
47  }
 48  
49  ------ ----------
 50 Disclaimer: this article is CSDN blogger "Twilight _" in the original article, follow the BY-4.0 CC SA copyright agreement, reproduced, please attach the original source link and this statement.
51 Original link: HTTPS: // blog.csdn.net/u012091092/article/details/45849445

 

2 to find universal law

2.1 Description

Start searching backwards from the last column, the first row of each column is the minimum of the column, when the target value is smaller than the first row of the column, which is smaller than all numbers in this row; found equal to the number of the column is greater than the target value, and record col;

Find the beginning of each row from the first column of the first row col, col first row of each column is the maximum value of the current line, to find the target value of less than equal to the number of column col, Row record number of downlink;

Find array.length performed between rows and columns row ~ 0 ~ col value to correspond; time complexity is o (n + m), the spatial complexity is o (1)

2.2code

. 1   public  Boolean the Find ( int target, int [] [] Array) {
 2          // of 0. The empty judgment 
. 3          IF (Array == null || 0 || be array.length == Array [0] == 0 .length ) {
 . 4              return  to false ;
 . 5          }
 . 6          @ 1. judged in the first column, the target element in the first row is compared with each column
 7          // elements of each column is the first row of each column of smallest 
. 8          int COL = 0 ;
 . 9          for ( int I = Array [0]. 1-.length; I> = 0; i-- ) {
 10              IF (target> = Array [. 1 ] [I]) {
 . 11                 = col I;
 12 is                  BREAK ;
 13 is              }
 14          }
 15          // 2. judged in the line, the target is compared with the first column of each row col
 16          // first column of each row col row is the largest 
. 17          int Row = 0 ;
 18 is          for ( int I = 0; I <be array.length; I ++ ) {
 . 19              IF (target> = Array [COL] [I]) {
 20 is                  Row = I;
 21 is                  BREAK ;
 22 is              }
 23 is          }
 24          // 3.target 0 ~ col is in between the row and be array.length 
25         for(int i=row;i<array.length;i++){
26             for (int j=0;j<=col;j++){
27                 if(array[i][j]==target){
28                     return true;
29                 }
30             }
31         }
32         return false;
33     }

 

Guess you like

Origin www.cnblogs.com/ERFishing/p/11831199.html
Recommended