题目描述
思路
- 二分
# """
# This is BinaryMatrix's API interface.
# You should not implement it, or speculate about its implementation
# """
#class BinaryMatrix(object):
# def get(self, row: int, col: int) -> int:
# def dimensions(self) -> list[]:
class Solution:
def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
m, n = binaryMatrix.dimensions() # row, column
left, right = 0, n-1
while left < right:
mid = left + (right - left) // 2
key = False
for i in range(m):
if binaryMatrix.get(i, mid):
key = True
break
if not key:
left = mid + 1
else:
right = mid
key = False
for i in range(m):
if binaryMatrix.get(i, left):
key = True
break
return left if key else -1
- T i m e : O ( n l o g m ) Time: O(nlogm) Time:O(nlogm) n 为列 m为行