forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (27 loc) · 996 Bytes
/
Solution.cs
File metadata and controls
29 lines (27 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace LeetCodeNet.G0001_0100.S0074_search_a_2d_matrix {
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
// #Udemy_2D_Arrays/Matrix #Top_Interview_150_Binary_Search #Big_O_Time_O(endRow+endCol)_Space_O(1)
// #2025_06_13_Time_0_ms_(100.00%)_Space_44.41_MB_(51.93%)
public class Solution {
public bool SearchMatrix(int[][] matrix, int target) {
int endRow = matrix.Length;
int endCol = matrix[0].Length;
int targetRow = 0;
bool result = false;
for (int i = 0; i < endRow; i++) {
if (matrix[i][endCol - 1] >= target) {
targetRow = i;
break;
}
}
for (int i = 0; i < endCol; i++) {
if (matrix[targetRow][i] == target) {
result = true;
break;
}
}
return result;
}
}
}