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

Commit ad9e0b1

Browse files
Add files via upload
1 parent eb2181c commit ad9e0b1

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+
class Solution
6+
{
7+
public:
8+
bool searchMatrix(vector<vector<int>>& matrix, int target)
9+
{
10+
int row = matrix.size();
11+
if (row == 0) return false;
12+
13+
int col = matrix[0].size();
14+
if (col == 0) return false;
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+
else if (matrix[rowIndex][colIndex] < target)
25+
{
26+
++rowIndex;
27+
}
28+
else
29+
{
30+
return true;
31+
}
32+
}
33+
34+
return false;
35+
}
36+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /