星空网 > 软件开发 > Java

[LeetCode] Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

     这道题还是很简单的。没啥可说的。直接用hashmap就可以做了,借助containsKey(),get()还有put()。

     代码如下。~

public class Solution {  public boolean containsNearbyDuplicate(int[] nums, int k) {  Map<Integer,Integer>test=new HashMap<>();  for(int i=0;i<nums.length;i++){    if(test.containsKey(nums[i])){      int diff=i-test.get(nums[i]);      if(diff<=k){        return true;      }    }    test.put(nums[i],i);  }  return false;      }}

 




原标题:[LeetCode] Contains Duplicate II

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

HAIMA海玛|品牌出海通过TikTok+KOL种草怎么打通转化路径?:https://www.kjdsnews.com/a/1479673.html
复购率太低?优化这个邮件,客户持续下单!:https://www.kjdsnews.com/a/1479674.html
亚马逊全球开店助力卖家夯实高质量跨境电商发展的四大核心能力:https://www.kjdsnews.com/a/1479675.html
阿里国际站代运营:企业常见的运营误区:https://www.kjdsnews.com/a/1479676.html
Google seo 如何做外贸推广?:https://www.kjdsnews.com/a/1479677.html
外贸建站公司:做谷歌外贸网站多少钱:https://www.kjdsnews.com/a/1479678.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流