-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path74.search-a-2d-matrix.py
More file actions
77 lines (70 loc) · 1.96 KB
/
74.search-a-2d-matrix.py
File metadata and controls
77 lines (70 loc) · 1.96 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet start
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
def row_search(low, high):
if low > high:
return -1
if matrix[low][0] <= target and target <= matrix[0][len(matrix[low]) - 1]:
return low
if matrix[high][0] <= target and target <= matrix[0][len(matrix[high]) - 1]:
return high
mid = (low + high) // 2
if matrix[mid][0] <= target and target <= matrix[mid][len(matrix[mid]) - 1]:
return mid
if matrix[mid][0] < target:
return row_search(mid + 1, high)
if matrix[mid][0] > target:
return row_search(low, mid - 1)
row = row_search(0, len(matrix) - 1)
nums = matrix[row]
def bin_search(low, high):
if low > high:
return -1
mid = (high + low) // 2
if nums[low] == target:
return low
if nums[high] == target:
return high
if nums[mid] == target:
return mid
if nums[mid] < target:
return bin_search(mid + 1, high)
if nums[mid] > target:
return bin_search(low, mid - 1)
isValid = bin_search(0, len(nums) - 1)
return isValid != -1
# @leet end