File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments