1、前言
![](https://img.haomeiwen.com/i11345146/3b8afadb021e289f.png)
2、思路
本题是找出第一个使得行或者列画满的格子。只需要依次涂,然后看每行或者每列满没满就行
3、代码
class Solution {
public int firstCompleteIndex(int[] arr, int[][] mat) {
Map<Integer, int[]> map = new HashMap<>();
int m = mat.length, n = mat[0].length;
for (int i = 0; i < m; i++) {
for(int j = 0; j < n; j++){
map.put(mat[i][j], new int[]{i, j});
}
}
int[] mArr = new int[m], nArr = new int[n];
for (int i = 0; i < arr.length; i++) {
int[] index = map.get(arr[i]);
// 一行中,每个列都画满了;一列中,每个行都画满了
if(++mArr[index[0]] == n || ++nArr[index[1]] == m){
return i;
}
}
return -1;
}
}
网友评论