File tree Expand file tree Collapse file tree 1 file changed +9
-8
lines changed
src/_240_SearchA2DMatrixII Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
- * Time : O(log(mn) ) ; Space: O(1)
2
+ * Time : O(m + n ) ; Space: O(1)
3
3
* @tag : Divide and Conquer; Binary Search
4
4
* @by : Steven Cooks
5
5
* @date: Aug 17, 2015
27
27
/** see test {@link _240_SearchA2DMatrixII.SolutionTest } */
28
28
public class Solution {
29
29
30
+ // start from top right corner
30
31
public boolean searchMatrix (int [][] matrix , int target ) {
31
32
if (matrix .length == 0 || matrix [0 ].length == 0 ) {
32
33
return false ;
33
34
}
34
35
int rows = matrix .length ;
35
36
int cols = matrix [0 ].length ;
36
- int j = 0 ;
37
- int i = cols - 1 ; ;
38
- while (j < rows && i >= 0 ) {
39
- if (matrix [j ][ i ] == target ) {
37
+ int i = 0 ;
38
+ int j = cols - 1 ;
39
+ while (i < rows && j >= 0 ) {
40
+ if (matrix [i ][ j ] == target ) {
40
41
return true ;
41
- } else if (matrix [j ][ i ] < target ) {
42
- j ++;
42
+ } else if (matrix [i ][ j ] < target ) {
43
+ i ++;
43
44
} else {
44
- i --;
45
+ j --;
45
46
}
46
47
}
47
48
return false ;
You can’t perform that action at this time.
0 commit comments