LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

本文解析了LeetCode上219题ContainsDuplicateII的题意及解决方案,介绍了三种方法:直接比较(运行超时)、使用HashMap和使用HashSet。通过对比不同方法的优劣,为读者提供了高效解决问题的思路。

1、题目名称

Contains Duplicate II(判断数组内是否有重复元素2)

2、题目地址

https://leetcode.com/problems/contains-duplicate-ii/

3、题目内容

英文: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.

中文:给出一个整数数组,判断该数组内是否有两个元素值是相同的,且他们的索引值相差不大于k,是则返回true,否则返回false

4、一个TLE的方法

本题如果直接使用暴力方法解决会运行超时。一段TLE的Java代码如下:

/**
 * @功能说明:LeetCode 219 - Contains Duplicate II
 * @开发人员:Tsybius2014
 * @开发时间:2015年10月15日
 */
public class Solution {
    
    /**
     * 查看数组内是否有重复元素且相邻重复元素索引间隔不大于K
     * @param nums
     * @return
     */
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        
        if (nums.length <= 1) {
            return false;
        }

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j <= i + k && j < nums.length; j++) {
                if (nums[i] == nums[j]) {
                    return true;
                }
            }
        }
        
        return false;
    }
}

5、解题方法1

一个比较容易想到的方式是使用HashMap来完成目标,使用HashMap解决本题的方式与解决第217题的方式(Contains Duplicate)非常类似。

Java代码如下:

import java.util.HashMap;

/**
 * @功能说明:LeetCode 219 - Contains Duplicate II
 * @开发人员:Tsybius2014
 * @开发时间:2015年10月15日
 */
public class Solution {
    
    /**
     * 查看数组内是否有重复元素且相邻重复元素索引间隔不大于K
     * @param nums
     * @return
     */
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        
        if (nums.length <= 1) {
            return false;
        }

        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(nums[i]) && i - hashMap.get(nums[i]) <= k) {
                return true;
            }
            hashMap.put(nums[i], i);
        }
        
        return false;
    }
}

6、解题方法2

另一种方式是使用HashSet来解决本问题。HashSet是使用HashMap实现的集合。在HashSet的add函数中,如果被插入的元素已存在,则返回true,否则返回false。下面的Java代码就是利用了HashSet的这个性质:

import java.util.HashSet;

/**
 * @功能说明:LeetCode 219 - Contains Duplicate II
 * @开发人员:Tsybius2014
 * @开发时间:2015年10月15日
 */
public class Solution {

    /**
     * 查看数组内是否有重复元素且相邻重复元素索引间隔不大于K
     * 
     * @param nums
     * @return
     */
    public boolean containsNearbyDuplicate(int[] nums, int k) {

        if (nums.length <= 1) {
            return false;
        }

        HashSet<Integer> hashSet = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (i > k) {
                hashSet.remove(nums[i - k - 1]);
            }
            if (!hashSet.add(nums[i])) {
                return true;
            }
        }

        return false;
    }
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/517511

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值