(This problem is an interactive problem.)
A binary matrix means that all elements are
0
or1
. For each individual row of the matrix, this row is sorted in non-decreasing order.
Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0-indexed) with at least a
1
in it. If such index doesn't exist, return-1
.
You can't access the Binary Matrix directly. You may only access the matrix using a
BinaryMatrix
interface:
BinaryMatrix.get(x, y)
returns the element of the matrix at index(x, y)
(0-indexed).BinaryMatrix.dimensions()
returns a list of 2 elements[n, m]
, which means the matrix isn * m
.
Submissions making more than
1000
calls toBinaryMatrix.get
will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.
For custom testing purposes you're given the binary matrix
mat
as input in the following four examples. You will not have access the binary matrix directly.
Constraints:
1 <= mat.length, mat[i].length <= 100
mat[i][j]
is either0
or1
.mat[i]
is sorted in a non-decreasing way.
(這是一個互動式問題)
一個二元陣列表示所有元素都是
0
或1
。陣列中每一個個別的row都經過非遞減排序。
給一個row都排序過的二元陣列,回傳
1
再最左邊的column索引值(從零開始算)。如不存在任何1
就回傳-1
。
你不能直接存取二元陣列。你只能使用
BinaryMatrix
的interface去使用:
BinaryMatrix.get(x, y)
回傳位於(x, y)
的元素(從零開始算)。BinaryMatrix.dimensions()
回傳包含兩個元素[n, m]
的list,代表該陣列大小為n * m
。
使用了超過
1000
次的BinaryMatrix.get將被判定錯誤,並且任何試圖走後門的方式都會被取消資格。
為了方便測試,下面提供了四個陣列。你不能直接存取它們。
限制:
1 <= mat.length, mat[i].length <= 100
mat[i][j]
只會是0
或1
.mat[i]
會按照非遞減順序排列。
x
y
給的感覺很奇怪,我一直搞反它們,因此這邊只講概念,下面的code雖然會過但請參考就好。1
一定在右邊,需要找到哪一排擁有最多的1
(1
在最左邊)。1
,往左走一格。0
,往下走一格。0
的地方,因此答案要加一。0
的陣列,即可成功。LeetCode
C++