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

Commit13baed2

Browse files
authored
Merge pull requestneetcode-gh#40 from abhayhub/patch-1
74 . Create Search a 2d matrix
2 parents116c55a +61bca3c commit13baed2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎cpp/74-Search a 2d matrix‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// OJ: https://leetcode.com/problems/search-a-2d-matrix/
2+
// Time: O(M + N)
3+
// Space: O(1)
4+
class Solution {
5+
public:
6+
bool searchMatrix(vector<vector<int>>& A, int target) {
7+
if (A.empty() || A[0].empty()) return false;
8+
int M = A.size(), N = A[0].size(), i = 0, j = N - 1;
9+
while (i < M && j >= 0) {
10+
if (A[i][j] == target) return true;
11+
if (A[i][j] < target) ++i;
12+
else --j;
13+
}
14+
return false;
15+
}
16+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp