Skip to content

Commit 8ac6a67

Browse files
committed
240 (2) update time complexity
1 parent bbf6b0f commit 8ac6a67

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/_240_SearchA2DMatrixII/Solution.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -27,21 +27,22 @@
2727
/** see test {@link _240_SearchA2DMatrixII.SolutionTest } */
2828
public 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;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy