-
Notifications
You must be signed in to change notification settings - Fork 1
/
Search a 2D Matrix II.py
38 lines (33 loc) · 1016 Bytes
/
Search a 2D Matrix II.py
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
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""
Created on Sat May 2 14:14:03 2020
@author: Anuj
"""
mat = [
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30],
]
class Solution:
def searchMatrix(self, mat, target):
if not len(mat) or not len(mat[0]):
return False
height = len(mat) #h
width = len(mat[0]) #w
for row in range(len(mat)):
if mat[row][0]<= target<=mat[row][-1]:
low = 0
high = width - 1
while low <= high:
middle = low + (high - low)//2
mid_value = mat[row][middle]
if target > mid_value:
low = middle + 1
elif target < mid_value:
high =middle - 1
else:
return True
return False
print(Solution.searchMatrix("", mat, 30))