題目:
寫出一個高效的算法來搜索 m × n矩陣中的值。
這個矩陣具有以下特性:
- 每行中的整數從左到右是排序的。
- 每行的第一個數大於上一行的最後一個整數。
鏈接:點擊打開鏈接
代碼:
暴力:
class Solution:
"""
@param matrix: matrix, a list of lists of integers
@param target: An integer
@return: a boolean, indicate whether matrix contains target
"""
def searchMatrix(self, matrix, target):
# write your code here
if matrix==None:
return False
m=len(matrix)
if(m==0) :
return False
n=len(matrix[0])
num=m-1
for i in range(m):
for j in range(n):
if matrix[i][j]==target :
return True
return False
二分法:暫時先不寫了