1428. 至少有一个 1 的最左端列
(这是一个交互题)
我们称只包含元素 0
或 1
的矩阵为二进制矩阵。矩阵中每个单独的行都按非递减顺序排序。
给定一个这样的二进制矩阵,返回至少包含一个 1
的最左端列的索引(从 0 开始)。如果这样的列不存在,返回 -1
。
您不能直接访问该二进制矩阵。你只可以通过 BinaryMatrix
接口来访问。
BinaryMatrix.get(row, col)
返回位于索引 (row, col)
(从 0 开始)的元素。BinaryMatrix.dimensions()
返回含有 2 个元素的列表 [rows, cols]
,表示这是一个 rows * cols
的矩阵。如果提交的答案调用 BinaryMatrix.get
超过 1000
次,则该答案会被判定为错误答案。提交任何试图规避判定机制的答案将会被取消资格。
下列示例中, mat
为给定的二进制矩阵。您不能直接访问该矩阵。
示例 1:
输入: mat = [[0,0],[1,1]] 输出: 0
示例 2:
输入: mat = [[0,0],[0,1]] 输出: 1
示例 3:
输入: mat = [[0,0],[0,0]] 输出: -1
示例 4:
输入: mat = [[0,0,0,1],[0,0,1,1],[0,1,1,1]] 输出: 1
提示:
rows == mat.length
cols == mat[i].length
1 <= rows, cols <= 100
mat[i][j]
只会是 0
或 1
。mat[i]
已按非递减顺序排序。原站题解
golang 解法, 执行用时: 8 ms, 内存消耗: 5 MB, 提交时间: 2023-10-22 10:14:58
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * type BinaryMatrix struct { * Get func(int, int) int * Dimensions func() []int * } */ func leftMostColumnWithOne(binaryMatrix BinaryMatrix) int { last := binaryMatrix.Dimensions() h,w := last[0]-1, last[1]-1 var find bool for w>=0 && h>=0{ if binaryMatrix.Get(h,w)==1{ find = true w-- }else{ h-- } } if !find{ return -1 } return w+1 }
golang 解法, 执行用时: 8 ms, 内存消耗: 5 MB, 提交时间: 2023-10-22 10:14:41
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * type BinaryMatrix struct { * Get func(int, int) int * Dimensions func() []int * } */ func leftMostColumnWithOne(binaryMatrix BinaryMatrix) int { group := binaryMatrix.Dimensions() rows, cols := group[0], group[1] l := make([]int, 0, rows) for row:=0; row<rows; row++ { if binaryMatrix.Get(row, cols-1) == 1 { l = append(l, row) } } if len(l) ==0 { return -1 } min := cols-1 for _,row:=range l { m := midSearch(binaryMatrix, row, 0, min) if m < min { min = m } } return min } func midSearch(b BinaryMatrix, row, start, end int) int { if start == end { return start } if b.Get(row, (start+end)/2) == 1 { return midSearch(b, row, start, (start+end)/2) } else { return midSearch(b, row, (start+end)/2 +1, end) } }
cpp 解法, 执行用时: 4 ms, 内存消耗: 8.5 MB, 提交时间: 2023-10-22 10:14:03
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * class BinaryMatrix { * public: * int get(int row, int col); * vector<int> dimensions(); * }; */ class Solution { public: // 利用单调递增性质,从右下到左上遍历,从右边遇到第一个为0的位置pos, 则pos左侧一定全为0 int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { int m = binaryMatrix.dimensions()[0], n = binaryMatrix.dimensions()[1]; int ans = n, i = m - 1, j = n - 1; while (i >= 0 && j >= 0) { while (j >= 0 && binaryMatrix.get(i, j)) j--; i--; } return j == n - 1 ? -1 : j + 1; // j = n - 1时,表示元素全部为0 } };
cpp 解法, 执行用时: 8 ms, 内存消耗: 8.3 MB, 提交时间: 2023-10-22 10:13:07
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * class BinaryMatrix { * public: * int get(int row, int col); * vector<int> dimensions(); * }; */ class Solution { public: // 二分法、对每一行进行二分, int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { int m = binaryMatrix.dimensions()[0], n = binaryMatrix.dimensions()[1]; int ans = n; for (int i = 0; i < m; i++) { int left = 0, right = n, mid; while (left < right) { // 找到第一个1的位置 mid = (left + right) >> 1; if (binaryMatrix.get(i, mid)) { right = mid; } else { left = mid + 1; } } if (left < n) ans = min(ans, left); // left = n时表示该行中不存在1 } return ans == n ? -1 : ans; } };
python3 解法, 执行用时: 108 ms, 内存消耗: 16.4 MB, 提交时间: 2023-10-22 10:12:39
# """ # 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: row, col = binaryMatrix.dimensions() i, j = 0, col - 1 while -1 < i < row and -1 < j < col: if binaryMatrix.get(i, j) == 1: j -= 1 else: i += 1 if j == col - 1: return -1 else: return j + 1
java 解法, 执行用时: 0 ms, 内存消耗: 42 MB, 提交时间: 2023-10-22 10:12:25
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * interface BinaryMatrix { * public int get(int row, int col) {} * public List<Integer> dimensions {} * }; */ class Solution { public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { List<Integer> d = binaryMatrix.dimensions(); int r = d.get(0) - 1; int c = d.get(1) - 1; int ans = -1; while (r >= 0){ if (binaryMatrix.get(r, c) == 1) { if (c == 0) return 0; ans = c; c --; } else r--; } return ans; } }