Contains Duplicate II

来源:互联网 发布:怎么添加js 编辑:程序博客网 时间:2024/05/16 18:23

请看清题意!!!是最多为k,而不是等于k

public class Solution {    public boolean containsNearbyDuplicate(int[] nums, int k) {        if (nums == null || nums.length < 2) {            return false;        }        Map<Integer, Integer> map = new HashMap<>();        for (int i = 0; i < nums.length; i++) {            if (map.containsKey(nums[i])) {                int index = map.get(nums[i]);                int sub = i - index;
<pre name="code" class="java"><span style="white-space:pre"></span>//if (sub == k) {
if (sub <= k) { return true; } else { map.put(nums[i], i); } } else { map.put(nums[i], i); } } return false; }}



0 0
原创粉丝点击