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

Commitad9e0b1

Browse files
Add files via upload
1 parenteb2181c commitad9e0b1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 剑指offer上的题目
2+
// Runtime: 60 ms, faster than 94.48% of C++ online submissions for Search a 2D Matrix II.
3+
// Memory Usage: 12.8 MB, less than 91.11% of C++ online submissions for Search a 2D Matrix II.
4+
5+
classSolution
6+
{
7+
public:
8+
boolsearchMatrix(vector<vector<int>>& matrix,int target)
9+
{
10+
int row = matrix.size();
11+
if (row ==0)returnfalse;
12+
13+
int col = matrix[0].size();
14+
if (col ==0)returnfalse;
15+
16+
int rowIndex =0, colIndex = col -1;
17+
18+
while (colIndex >=0 && rowIndex < row)
19+
{
20+
if (matrix[rowIndex][colIndex] > target)
21+
{
22+
--colIndex;
23+
}
24+
elseif (matrix[rowIndex][colIndex] < target)
25+
{
26+
++rowIndex;
27+
}
28+
else
29+
{
30+
returntrue;
31+
}
32+
}
33+
34+
returnfalse;
35+
}
36+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp