Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8ac6a67

Browse files
committed
240 (2) update time complexity
1 parentbbf6b0f commit8ac6a67

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
publicclassSolution {
2929

30+
// start from top right corner
3031
publicbooleansearchMatrix(int[][]matrix,inttarget) {
3132
if (matrix.length ==0 ||matrix[0].length ==0) {
3233
returnfalse;
3334
}
3435
introws =matrix.length;
3536
intcols =matrix[0].length;
36-
intj =0;
37-
inti =cols -1;;
38-
while (j <rows &&i >=0) {
39-
if (matrix[j][i] ==target) {
37+
inti =0;
38+
intj =cols -1;
39+
while (i <rows &&j >=0) {
40+
if (matrix[i][j] ==target) {
4041
returntrue;
41-
}elseif (matrix[j][i] <target) {
42-
j++;
42+
}elseif (matrix[i][j] <target) {
43+
i++;
4344
}else {
44-
i--;
45+
j--;
4546
}
4647
}
4748
returnfalse;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp