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 11/**
2- * Time : O(log(mn) ) ; Space: O(1)
2+ * Time : O(m + n ) ; Space: O(1)
33 * @tag : Divide and Conquer; Binary Search
44 * @by : Steven Cooks
55 * @date: Aug 17, 2015
2727/** see test {@link _240_SearchA2DMatrixII.SolutionTest } */
2828public class Solution {
2929
30+ // start from top right corner
3031 public boolean searchMatrix (int [][] matrix , int target ) {
3132 if (matrix .length == 0 || matrix [0 ].length == 0 ) {
3233 return false ;
3334 }
3435 int rows = matrix .length ;
3536 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 ) {
4041 return true ;
41- } else if (matrix [j ][ i ] < target ) {
42- j ++;
42+ } else if (matrix [i ][ j ] < target ) {
43+ i ++;
4344 } else {
44- i --;
45+ j --;
4546 }
4647 }
4748 return false ;
You can’t perform that action at this time.
0 commit comments