mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-12 12:51:10 +00:00
Merge branch 'main' into swift
# Conflicts: # animation-simulation/数组篇/leetcode219数组中重复元素2.md # animation-simulation/数组篇/leetcode59螺旋矩阵2.md # animation-simulation/数组篇/leetcode66加一.md # animation-simulation/数组篇/leetcode75颜色分类.md
This commit is contained in:
@@ -78,6 +78,22 @@ class Solution:
|
||||
return False
|
||||
```
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Swift Code
|
||||
|
||||
```swift
|
||||
@@ -165,7 +181,24 @@ class Solution:
|
||||
if len(s) > k:
|
||||
s.remove(nums[i - k])
|
||||
return False
|
||||
```
|
||||
```
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Swift Code
|
||||
|
||||
@@ -190,4 +223,4 @@ class Solution {
|
||||
return false
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
Reference in New Issue
Block a user