Array two-dimensional array lookup

Posted by tate on Thu, 20 Jan 2022 02:37:52 +0100

In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom.
Please complete a function, enter such a two-dimensional array and an integer, and judge whether the array contains the integer.
[
[1,2,8,9],
[2,4,9,12],
[4,7,10,13],
[6,8,11,15]
]
Given target = 7, return true.
Given target = 3, false is returned.

Method 1:
Note: because the array increases from left to right and from top to bottom, we use two diagonals to find it. The lower left corner is the largest number in all rows and the smallest number in all columns, so we use this feature. You can find the number you want according to the boundary between row and column
For example:
1 3 4 6
2 5 7 9
10 12 14 16
11 13 15 17
In this group of numbers, if you want to find the number in the lower left corner first, you should first find the subscript in the lower left corner. The row subscript in the lower left corner is the length of the array - 1 and the column subscript is 0. Therefore, judge from two directions, one row direction and one column direction. First, from the row, it is the smallest in this row. Therefore, if the number to be found is smaller than this number, it must not be found in this row. Then find it in a new row. Only the number above it is smaller than him. If the number to be found is larger than this number, it must not be found in the first column, Because this number is the largest number in the first column, you can only find it by changing columns. Therefore, according to this principle, the algorithm implementation can be written:

public static boolean ChaZhao(int target, int [][] array) {
             //Conditional procedure for judging whether it is empty
	        int rows = array.length;    //Here, the length of the two-dimensional array is defined, that is, the maximum value of the row subscript + 1
	        if(rows == 0){      //Judge whether rows is 0. If it is zero, there are no elements in the whole array, and false is returned
	            return false;
	        }
	        int cols = array[0].length; //Define the length of one-dimensional array, that is, the maximum value in the following table + 1
	        if(cols == 0){  //Judge whether the column length is 0. If the column length is 0, there are no elements in the array, so false is returned
	            return false;
	        }
	        // According to the characteristics of rows and columns, start from the "bottom left" to find the number you want
	        int row = rows-1;//Lower left row subscript
	        int col = 0;//Lower left column subscript
	        while(row>=0 && col<cols){  //Because the row is searched from the last row, it will be searched in descending order. Therefore, a range is required to specify the value that the row can take. The last row must be greater than or equal to 0. The column subscript is searched from the first column, so the maximum column subscript cannot exceed the last column
	            if(array[row][col] < target){    //If the number is less than the target number, change the column
	                col++;
	            }else if(array[row][col] > target){//If the number is greater than the target number, find a new line
	                row--;
	            }else{
	                return true;//Until you find the number you want to find, and finally return true
	            }
	        }
	        return false;//Otherwise, false is returned
	    }

Find from the top right, similar to the bottom left:

public static boolean ChaZhao(int target, int [][] array) {
        int rows = array.length;
        if(rows == 0){
            return false;
        }
        int cols = array[0].length;
        if(cols == 0){
            return false;
        }
        // Upper right
        int row = 0;
        int col = cols-1;
        while(row<rows&& col>=0){
            if(array[row][col] < target){
                row++;
            }else if(array[row][col] > target){
                col--;
            }else{
                return true;
            }
        }
        return false;
    }

Method 2:
Brute force cracking:
Use a two-layer loop to look up one by one to see if there is the number.

 public static boolean Find(int target, int [][] array) {
       for(int i=0;i<array.length;i++){
           for(int j=0;j<array[0].length;j++){
               if(array[i][j] == target){
                   return true;
               }
           }
       }
       return false;
   }

Main function:

 public static void main(String[] args) {
	  int arr[][]=new int[4][4];
	  Scanner sc=new Scanner(System.in);
	  for(int i=0;i<arr.length;i++) {
		  for(int j=0;j<arr[0].length;j++) {
			  arr[i][j]=sc.nextInt();
		  }
	  }
	boolean x=ChaZhao(7,arr);
	System.out.println(x);
	  
}

result:
1 2 3 4
5 6 8 9
10 11 12 13
14 15 16 17
false

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
true