219.contains-duplicate-ii.md 2.9 KB
Newer Older
Y
yulecc 已提交
1
## 题目地址(219. 存在重复元素 II)
L
lucifer 已提交
2

Y
yulecc 已提交
3
https://leetcode-cn.com/problems/contains-duplicate-ii/
L
luzhipeng 已提交
4 5 6 7

## 题目描述

```
Y
yulecc 已提交
8
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的 绝对值 至多为 k。
L
luzhipeng 已提交
9

Y
yulecc 已提交
10
 
L
luzhipeng 已提交
11

Y
yulecc 已提交
12
示例 1:
L
luzhipeng 已提交
13

Y
yulecc 已提交
14 15 16
输入: nums = [1,2,3,1], k = 3
输出: true
示例 2:
L
luzhipeng 已提交
17

Y
yulecc 已提交
18 19 20 21 22 23
输入: nums = [1,0,1,1], k = 1
输出: true
示例 3:

输入: nums = [1,2,3,1,2,3], k = 2
输出: false
L
luzhipeng 已提交
24 25 26

```

L
lspeer 已提交
27 28 29
## 前置知识

- hashmap
30 31 32 33 34 35 36

## 公司

- 阿里
- 腾讯
- 百度
- 字节
L
lucifer 已提交
37

L
luzhipeng 已提交
38 39
## 思路

L
lucifer 已提交
40 41 42
用一个 hashmap 存储已经访问过的数字,每次访问都查看 hashmap 中是否有这个元素,有的话拿出索引进行比对,是否满足条件(相隔不大于 k),如果满足返回 true 即可。

可以看出,这道题就是两数和的进阶版。大家可以将这两道题结合起来理解哦~
L
lucifer 已提交
43

L
lucifer 已提交
44
## 公司
L
luzhipeng 已提交
45

L
lucifer 已提交
46 47
- airbnb
- palantir
L
luzhipeng 已提交
48 49 50

## 关键点解析

L
lucifer 已提交
51
- 空间换时间
L
luzhipeng 已提交
52 53 54

## 代码

L
lucifer 已提交
55
- 语言支持:JS,Python,C++, Java
56 57 58

Javascript Code:

L
luzhipeng 已提交
59 60 61 62 63 64
```js
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
L
lucifer 已提交
65 66 67 68 69 70
var containsNearbyDuplicate = function (nums, k) {
  const visited = {};
  for (let i = 0; i < nums.length; i++) {
    const num = nums[i];
    if (visited[num] !== undefined && i - visited[num] <= k) {
      return true;
L
luzhipeng 已提交
71
    }
L
lucifer 已提交
72 73 74
    visited[num] = i;
  }
  return false;
L
luzhipeng 已提交
75 76 77
};
```

78 79 80 81 82 83 84 85 86 87 88 89
Python Code:

```python
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        d = {}
        for index, num in enumerate(nums):
            if num in d and index - d[num] <= k:
                return True
            d[num] = index
        return False
```
90

91
C++ Code:
L
lucifer 已提交
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
```C++
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        auto m = unordered_map<int, int>();
        for (int i = 0; i < nums.size(); ++i) {
            auto iter = m.find(nums[i]);
            if (iter != m.end()) {
                if (i - m[nums[i]] <= k) {
                    return true;
                }
            }
            m[nums[i]] = i;
        }
        return false;
    }
};
```
L
lucifer 已提交
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
Java Code:

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

L
lucifer 已提交
131 132
**复杂度分析**

L
lucifer 已提交
133 134
- 时间复杂度:$O(N)$
- 空间复杂度:$O(N)$
L
lucifer 已提交
135

L
lucifer 已提交
136
更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。
L
lucifer 已提交
137

L
lucifer 已提交
138
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
L
lucifer 已提交
139 140

![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)