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 < n; i++){
for(int j = 0; j < m; j++){
if(map.containsKey(mat[i][j])){
for(Cell c : map.get(mat[i][j])){
int Dist = Math.abs(i - c.row)+Math.abs(j - c.col);
if(Dist <= k){
return true;
}
if(i - c.row > 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 < arr.length; i++) {
// If already present n hash, then we found
// a duplicate within k distance
if (set.contains(arr[i]))
return true;
// Add this item to hashset
set.add(arr[i]);
// Remove the k+1 distant item
if (i >= k)
set.remove(arr[i - k]);
}
return false;
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64971.html
I complete reading JavaScript Enlightenment recently. The book is more about basics in JavaScript and suitable for beginners. Here are a list of my benefits and extensions from the book. Math JavaScri...
摘要:我的博客大家都知道解决了回调地狱的问题。这就是异步的嵌套带来的可读性的问题,它是由异步的运行机制引起的。在与第三方团队沟通之后问题得到了解决。这不但使代码变得臃肿不堪,还进一步加剧了可读性的问题。的特征保证了可以解决信任问题。 我的github博客 https://github.com/zhuanyongxigua/blog 大家都知道Promise解决了回调地狱的问题。说到回调地狱,...
摘要:为了控制压测时的,则需要实现逻辑。则是获取属性并初始化客户端客户端配置则提供了设置泛化调用入参的以及接下来要介绍的部分的全链路压测中,我们都使用校验请求结果,压测插件中,我们也实现了基于的校验。 Dubbo 压测插件已开源,本文涉及代码详见gatling-dubbo Gatling 是一个开源的基于 Scala、Akka、Netty 实现的高性能压测框架,较之其他基于线程实现的压测框架...
摘要:为了控制压测时的,则需要实现逻辑。则是获取属性并初始化客户端客户端配置则提供了设置泛化调用入参的以及接下来要介绍的部分的全链路压测中,我们都使用校验请求结果,压测插件中,我们也实现了基于的校验。 Dubbo 压测插件已开源,本文涉及代码详见gatling-dubbo Gatling 是一个开源的基于 Scala、Akka、Netty 实现的高性能压测框架,较之其他基于线程实现的压测框架...
阅读 3247·2021-11-16 11:45
阅读 6131·2021-09-22 10:57
阅读 2222·2021-09-08 09:36
阅读 2164·2021-09-02 15:40
阅读 2762·2021-07-26 23:38
阅读 1475·2019-08-30 15:55
阅读 1151·2019-08-30 15:54
阅读 1452·2019-08-29 14:06