LEETCODE 1428. Leftmost Column with at Least a One 解题思路分析

（这是一道互动问题）

• `BinaryMatrix.get(row, col)` 返回第row行第col列上的元素。（0-based）
• `BinaryMatrix.dimensions()` 返回一个列表[rows, cols]，代表矩阵拥有rows行以及cols列。

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.

Example 1:

```Input: mat = [[0,0],[1,1]]
Output: 0```

Example 2:

```Input: mat = [[0,0],[0,1]]
Output: 1```

Example 3:

```Input: mat = [[0,0],[0,0]]
Output: -1```

Example 4:

```Input: mat = [[0,0,0,1],[0,0,1,1],[0,1,1,1]]
Output: 1```

Constraints:

• `rows == mat.length`
• `cols == mat[i].length`
• `1 <= rows, cols <= 100`
• `mat[i][j]` is either `0` or `1`.
• `mat[i]` is sorted in a non-decreasing way.

```int rows, cols;
public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) {
List<Integer> list = binaryMatrix.dimensions();
rows=list.get(0);
cols=list.get(1);
int minCol=cols;
for(int row=0;row<rows;row++){
minCol=Math.min(minCol,findFirstOne(binaryMatrix,row));
}
return minCol==cols?-1:minCol;
}

int findFirstOne(BinaryMatrix binaryMatrix, int row){
int low=0,high=cols-1;
while(low<=high){
int mid=(low+high)/2;
if(binaryMatrix.get(row,mid)==1){
high=mid-1;
}else{
low=mid+1;
}
}
return low;
}```

Runtime: 0 ms, faster than 100.00% of Java online submissions for Leftmost Column with at Least a One.

Memory Usage: 40 MB, less than 100.00% of Java online submissions for Leftmost Column with at Least a One.