File tree Expand file tree Collapse file tree 1 file changed +17
-11
lines changed Expand file tree Collapse file tree 1 file changed +17
-11
lines changed Original file line number Diff line number Diff line change 11class Solution {
22 public boolean searchMatrix (int [][] matrix , int target ) {
3- int i = 0 ;
4- int j = matrix [0 ].length - 1 ;
5- while (i >= 0 && i <= matrix .length - 1 && j >= 0 && j <= matrix [0 ].length - 1 ){
6- if (matrix [i ][j ] == target ){
7- return true ;
8- } else if (matrix [i ][j ] > target ){
9- j --;
10- } else {
11- i ++;
12- }
3+ int lrow = 0 ;
4+ int rrow = matrix .length - 1 ;
5+ int n = 0 ;
6+
7+ while (lrow < rrow ) {
8+ n = (lrow + rrow ) / 2 ;
9+ if (matrix [n ][0 ] > target )
10+ rrow = n ;
11+ else if (matrix [n ][matrix [0 ].length - 1 ] < target )
12+ lrow = n + 1 ;
13+ else
14+ break ;
1315 }
14- return false ;
16+
17+ if (Arrays .binarySearch (matrix [(lrow + rrow ) / 2 ], target ) >= 0 )
18+ return true ;
19+ else
20+ return false ;
1521 }
1622}
You can’t perform that action at this time.
0 commit comments