def sort2(matrix,target):
for i in matrix:
if target in i:
return True
return False
def sort2_(matrix,target):
w,h = len(matrix),len(matrix[0])
for i in range(w):
for j in range(h):
if matrix[i][j] == target:
return True
return False
class Solution(object):
def binary_sort2(self, matrix, target):
w, h = len(matrix), len(matrix[0])
if not (w and h):
return False
left = 0
right = w*h-1
while left<=right:
mid = (left+right)//2
i,j = mid//w,mid%w #mid 的二维下标
tar = matrix[i][j]
if tar == target:
return True
elif tar < target:
left =mid +1
else:
right =mid -1
else :
return False
class Solution:
def twoSum(self,nums,target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
n = len(nums)
for x in range(n):
if target - nums[x] in d:
return d[target-nums[x]],x
else:
d[nums[x]] = x