Find numbers in a two-dimensional array (two-dimensional array with orderly increasing dimensions)

1: The theme of the program

1. Subject:

  In a two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.

 

2. Program written by yourself

 1 package com.jianke.it;
 2 
 3 public class FindNumFromTwoArray {
 4     /*
 5     [1,2,8,9],
 6     [2,4,9,12],
 7     [4,7,10,13],
 8     [6,8,11,15]
 9     */
10     public static void main(String[] args) {
11         long start=System.nanoTime();
12         int array[][]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
13         int target=7;
14         boolean flag=Find(target,array);
15         long end=System.nanoTime();
16         System.out.println("boolean="+flag);
17         System.out.println("spend time:"+(end-start)+"ns");
18     }
19     public static boolean Find(int target, int [][] array) {
20         boolean flag=false;
21         int count=array.length;
22         for(int i=0;i<count;i++) {
23             for(int j=0;j<array[i].length;j++) {
24                 if(target==array[i][j]) {
25                     return true;
26                 }
27             }
28         }
29         return flag;
30     }
31 
32 }

 

3. Results

  

 

4. Procedure 2

/* idea
* The matrix is ​​ordered, from the lower left corner, the upward number decreases, and the right number increases,
* So start searching from the lower left corner, when the number to be found is larger than the number in the lower left corner. move right
* To find a number that is less than the number in the lower left corner, move up
*/
 1 package com.jianke.it;
 2 
 3 public class FindNumFromTwoArray {
 4     /*
 5     [1,2,8,9],
 6     [2,4,9,12],
 7     [4,7,10,13],
 8     [6,8,11,15]
 9     */
10     public static void main(String[] args) {
11         long start=System.nanoTime();
12         int array[][]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
13         int target=7;
14         boolean flag=Find(target,array);
15         long end=System.nanoTime();
16         System.out.println("boolean="+flag);
17         System.out.println("spend time:"+(end-start)+"ns");
18     }
19     public static boolean Find(int target, int [][] array) {
20         int len=array.length-1;
21         int i=0;
22         while(len>=0&&(array[0].length>i)) {
23             if(array[len][0]>target) {
24                 len--;
25             }else if(array[len][i]<target){
26                 i++;
27             }else {
28                 return true;
29             }
30         }
31         return false;
32     }
33 
34 }

 

5. Procedure three

/* idea

*把每一行看成有序递增的数组,
*利用二分查找,
*通过遍历每一行得到答案,
*时间复杂度是nlogn

*/

 1 package com.jianke.it;
 2 
 3 public class FindNumFromTwoArray {
 4     /*
 5     [1,2,8,9],
 6     [2,4,9,12],
 7     [4,7,10,13],
 8     [6,8,11,15]
 9     */
10     public static void main(String[] args) {
11         long start=System.nanoTime();
12         int array[][]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
13         int target=7;
14         boolean flag=Find(target,array);
15         long end=System.nanoTime();
16         System.out.println("boolean="+flag);
17         System.out.println("spend time:"+(end-start)+"ns");
18     }
19     public static boolean Find(int target, int [][] array) {
20         for(int i=0;i<array.length;i++) {
21             int low=0;
22             int high=array[i].length-1;        
23             while(low<=high) {
24                 int mid=(low+high)/2;
25                 if(target<array[i][mid]) {
26                     high=mid-1;
27                 }else if(target>array[i][mid]) {
28                     low=mid+1;
29                 }else {
30                     return true;
31                 }
32             }
33         }
34         return false;
35     }
36 
37 }

 

Two: thinking

1. Why search from the bottom left corner

  Why not start the search from the upper left corner, and the upper left corner will increase to the right and down, then for a point, there will be a fork in the right and down;

  If we choose to start the search from the lower left foot, if it is greater than it will go to the right, if it is less than it will go down

 

2. Time complexity

  Method 1: Search from the bottom left, move up when large, move right when small, time complexity O(m+n)

  Method 2: Perform binary search for each row, time complexity O(mlogn)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325390493&siteId=291194637