leetcode 219. Contains Duplicate II

来源:互联网 发布:淘宝网首页免费注册 编辑:程序博客网 时间:2024/05/12 09:31

题意:在整数数组里面找到两个相等的数,并且下标的差不大于k,如果存在就返回true,否则返回false


解题思路:

依然是HashMap的应用


代码

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


0 0
原创粉丝点击