2019-09-13
Title Description
(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.
Problem-solving ideas
From the subject point of view, this two-dimensional arrays are ordered, from the top right to see, to the left decreasing, increasing downward.
-
- From the upper right arr [1] [j] starts looking for, if arr [1] [j]> N, then the left;
- If arr [1] [j] <N, then down;
- If it exceeds the boundary, then N is not in the two-dimensional array;
Reference Code
1 public class Solution { 2 public boolean Find(int target, int [][] array) { 3 if(array.length == 0 || array[0].length == 0) { 4 return false; 5 } 6 int m = array.length - 1; 7 int n = 0; 8 int temp = array[n][m]; 9 while(temp != target) { 10 if(m > 0 && n < array.length - 1) { 11 if(temp < target) { 12 n++; 13 } 14 if(temp > target) { 15 m--; 16 } 17 temp = array[n][m]; 18 }else { 19 return false; 20 } 21 } 22 return true; 23 } 24 }