Topic Description
In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in the order of increasing from left to right, and each column is sorted in the order of increasing from top to bottom. Please complete a function, input such a two-dimensional array and an integer, to determine whether the array contains the integer.
public class Solution { public boolean Find(int target, int [][] array) { } }
We can understand that there is a two-dimensional array, which increases from left to right and decreases from top to bottom. We need to find a specific value targer in this two-dimensional array.
We can have three solutions.
Violence law
We simply traverse the entire array to find the targer.
//Violence law public static boolean Find(int target, int [][] array) { //In order to be rigorous, it's better to judge whether the parameters passed are empty first when doing this kind of programming. if(array == null || array.length==0 || array[0].length==0){ return false; } //Traversing through each value for(int i=0;i<array.length;i++){ //i represents the row tree for(int j=0;j<array[0].length;j++){ //j denotes the number of columns in which it is located if(target==array[i][j]){ //Judging whether there is a value target return true; } } } //At the end of the traversal, no target value is found, and false is returned. return false; }
Dichotomous search method
In addition to violence laws, we can think about simpler ways to optimize our search for each line.
For arrays with small amount of data, there is little difference between the two, but when the amount of data is large, binary search is a better choice.
(Binary lookup is based on sequential storage structure. If we encounter sequential storage structure, we might consider Binary lookup.)
//Dichotomous search method public static boolean Find(int target, int [][] array) { //In order to be rigorous, it's better to judge whether the parameters passed are empty first when doing this kind of programming. int m = array.length; //Array rows int n = array[0].length; //Array column number if(array == null || m==0 || n==0){ return false; } //Traveling line by line for(int i=0;i<m;i++){ //Determine whether target may exist in the current row if(target >= array[i][0] && target <= array[i][n-1]){ //Binary search int left=0; //Define the left pointer int right = n-1; //Define the right pointer while(left<=right){ System.out.println(left+""+right); int mid = (right+left)/2; //Intermediate value if(target>array[i][mid]){ left=mid+1; } else if(target<array[i][mid]){ right=mid-1; } else{ return true; } } } } //At the end of the traversal, no target value is found, and false is returned. return false; }
Look up from the bottom left (top right)
When we get to this topic, the first idea must be to start looking up from the top left corner, but we find that we can't control the array subscript, because from left to right, from top to bottom are incremental, so we begin to think about whether there is a direction that makes the array increase in the horizontal and vertical direction, a decrease in order to control. Array subscripts are searched.
So we have the idea of looking from the bottom left (top right).
Suggestion: When doing this kind of questions, think first, when you have an idea, go on according to your own ideas. If you find problems in your own ideas, try to improve your own ideas, think more, try more, and develop a way of thinking to discover and solve problems. It will be very helpful for the future development.
/* Using the law of increasing two-dimensional arrays from top to bottom and from left to right, Then select the element a[row][col] in the lower left or upper right corner to compare with target. When the target is smaller than element a[row][col], then the target must be above the line where element a is located. row--; When the target is larger than element a[row][col], then the target must be on the right side of the column where element a is located. col++; */ //Look up from the bottom left public static boolean Find(int target, int [][] array) { //In order to be rigorous, it's better to judge whether the parameters passed are empty first when doing this kind of programming. if (array == null || array.length == 0 || (array.length == 1 && array[0].length == 0)) return false; //Start at the bottom left corner of the array and find the coordinates of the elements in the bottom left corner. int row = array.length-1; int col = 0; //Start traversing while(row >= 0 && col <= array[0].length-1){ //When the target is smaller than element a[row][col], then the target must be above the line where element a is located, that is row--; if (target < array[row][col]){ row--; } //When the target is larger than element a[row][col], then the target must be on the right side of the column where element a is located, that is, col++; else if(target > array[row][col]){ col++; } //Find the target and return to true else{ return true; } } //At the end of the traversal, no target value is found, and false is returned. return false; }