Check some
public static boolean checkDuplicateWithinK(int[][] mat, int k){
class Cell{
int row;
int col;
public Cell(int r, int c){
this.row = r;
this.col = c;
}
}
int n = mat.length;
int m = mat[0].length;
k = Math.min(k, n*m);
//map from distance to cell postions of the matrix
Map> map = new HashMap>();
for(int i = 0; i k){
map.remove(c);
}
}
map.get(mat[i][j]).add(new Cell(i, j));
}
else{
map.put(mat[i][j], new HashSet());
map.get(mat[i][j]).add(new Cell(i, j));
}
}
}
return false;
}
public boolean checkDuplicatesWithinK(int[][] matrix, int k) {
//convert matrix to an array
int[] arr = Arrays.stream(matrix)
.flatMapToInt(Arrays::stream)
.toArray();
// Creates an empty hashset
HashSet set = new HashSet();
// Traverse the input array
for (int i = 0; i = k)
set.remove(arr[i - k]);
}
return false;
}
关键字:java, int, mat, arr
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!