algorithm-base/animation-simulation/数组篇/leetcode219数组中重复元素2.md

226 lines
6.5 KiB
Java
Raw Normal View History

2021-03-20 08:30:29 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 07:48:03 +00:00
### [219 2](https://leetcode-cn.com/problems/contains-duplicate-ii/)
2021-03-17 11:49:19 +00:00
****
k i j使 nums [i] = nums [j] i j k
1:
> : nums = [1,2,3,1], k = 3
> : true
2:
> : nums = [1,0,1,1], k = 1
> : true
3:
> : nums = [1,2,3,1,2,3], k = 2
> : false
**Hashmap**
K HashMap
Java Code:
2021-03-17 11:49:19 +00:00
```java
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
//特殊情况
if (nums.length == 0) {
return false;
}
// hashmap
HashMap<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
// 如果含有
if (map.containsKey(nums[i])) {
//判断是否小于K如果小于等于则直接返回
2021-03-17 11:49:19 +00:00
int abs = Math.abs(i - map.get(nums[i]));
if (abs <= k) return true;//小于等于则返回
2021-03-17 11:49:19 +00:00
}
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
map.put(nums[i],i);
}
return false;
}
}
```
Python3 Code:
```python
from typing import List
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int)->bool:
#
if len(nums) == 0:
return False
#
m = {}
for i in range(0, len(nums)):
#
if nums[i] in m.keys():
# K
a = abs(i - m[nums[i]])
if a <= k:
return True#
#
m[nums[i]] = i
return False
```
2021-07-15 15:14:17 +00:00
C++ Code:
```cpp
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map <int, int> m;
for(int i = 0; i < nums.size(); ++i){
if(m.count(nums[i]) && i - m[nums[i]] <= k) return true;
m[nums[i]] = i;
}
return false;
}
};
```
2021-07-17 04:13:15 +00:00
Swift Code
```swift
class Solution {
func containsNearbyDuplicate(_ nums: [Int], _ k: Int) -> Bool {
if nums.count == 0 {
return false
}
var dict:[Int:Int] = [:]
for i in 0..<nums.count {
// 如果含有
if let v = dict[nums[i]] {
// 判断是否小于K如果小于等于则直接返回
let abs = abs(i - v)
if abs <= k {
return true
}
}
// 更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
dict[nums[i]] = i
}
return false
}
}
```
2021-03-17 11:49:19 +00:00
**HashSet**
****
K true
![leetcode2192](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/leetcode219数组中重复元素2.6m947ehfpb40.gif)
****
Java Code
2021-03-17 11:49:19 +00:00
```java
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
//特殊情况
if (nums.length == 0) {
return false;
}
// set
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; ++i) {
//含有该元素返回true
if (set.contains(nums[i])) {
return true;
}
// 添加新元素
set.add(nums[i]);
//维护窗口长度
if (set.size() > k) {
set.remove(nums[i-k]);
}
}
return false;
}
}
```
Python3 Code:
```python
from typing import List
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int)->bool:
#
if len(nums) == 0:
return False
#
s = set()
for i in range(0, len(nums)):
# True
if nums[i] in s:
return True
#
s.add(nums[i])
#
if len(s) > k:
s.remove(nums[i - k])
return False
2021-07-15 15:14:17 +00:00
```
C++ Code:
```cpp
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
multiset <int> S;
for(int i = 0; i < nums.size(); ++i){
if(S.count(nums[i])) return true;
S.insert(nums[i]);
if(S.size() > k) S.erase(nums[i - k]);
}
return false;
}
};
```
2021-07-17 04:13:15 +00:00
Swift Code
```swift
class Solution {
func containsNearbyDuplicate(_ nums: [Int], _ k: Int) -> Bool {
if nums.count == 0 {
return false
}
var set:Set<Int> = []
for i in 0..<nums.count {
// 含有该元素返回true
if set.contains(nums[i]) {
return true
}
// 添加新元素
set.insert(nums[i])
if set.count > k {
set.remove(nums[i - k])
}
}
return false
}
}
```