LeetCode 219. Contains Duplicate II

来源:互联网 发布:电脑视频文件修复软件 编辑:程序博客网 时间:2024/05/20 06:53
public class Solution {    public boolean containsNearbyDuplicate(int[] nums, int k) {        if (nums.length < 2) return false;    Map<Integer, Integer> map = new HashMap<Integer, Integer>();    for (int i = 0; i < nums.length; i++) {    int n = nums[i];if (map.containsKey(n)) {    if (i - map.get(n) <= k) return true;    else map.replace(n, i);    } else map.put(n, i);    }    return false;    }}

0 0
原创粉丝点击