国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Contains Duplicate II

2019-11-15 01:10:30
字體:
來源:轉載
供稿:網友
[LeetCode] Contains Duplicate II

Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and the difference betweeniandjis at mostk.

這道題還是很簡單的。沒啥可說的。直接用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;            }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇坪县| 尼木县| 绥芬河市| 城口县| 柏乡县| 合阳县| 平顶山市| 锡林郭勒盟| 德钦县| 库车县| 久治县| 敦化市| 息烽县| 靖远县| 且末县| 敦煌市| 正安县| 宝山区| 汉寿县| 湖北省| 阳城县| 黄浦区| 电白县| 海城市| 南和县| 资中县| 亚东县| 满城县| 常山县| 岳阳县| 宜君县| 龙陵县| 攀枝花市| 房山区| 辽宁省| 咸阳市| 定襄县| 淮阳县| 西城区| 武宁县| 娄底市|