Skip to content

Commit 37e2116

Browse files
Create 0074M. Search a 2D Matrix.py
1 parent df938d5 commit 37e2116

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

0074M. Search a 2D Matrix.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Runtime: 36 ms, faster than 95.27% of Python3 online submissions for Search a 2D Matrix.
2+
#Memory Usage: 14.7 MB, less than 86.50% of Python3 online submissions for Search a 2D Matrix.
3+
4+
class Solution:
5+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
6+
i_index = -2
7+
if target > matrix[-1][-1]:
8+
return False
9+
for i in range(len(matrix)):
10+
if target == matrix[i][0]:
11+
return True
12+
if target < matrix[i][0]:
13+
i_index = i - 1
14+
break
15+
if i_index == -2:
16+
i_index = len(matrix) - 1
17+
if i_index == -1:
18+
return False
19+
for i in range(len(matrix[0])):
20+
if target == matrix[i_index][i]:
21+
return True
22+
if target < matrix[i_index][i]:
23+
return False
24+
return False

0 commit comments

Comments
 (0)