Skip to content

feat: update solutions to lc problem: No.0219 #4001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions solution/0200-0299/0219.Contains Duplicate II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ tags:

### 方法一:哈希表

我们用哈希表 $d$ 存放最近遍历到的数以及对应的下标。
我们用一个哈希表 $\textit{d}$ 存放最近遍历到的数以及对应的下标。

遍历数组 `nums`,对于当前遍历到的元素 $nums[i]$,如果在哈希表中存在,并且下标与当前元素的下标之差不超过 $k$,则返回 `true`,否则将当前元素加入哈希表中。
遍历数组 $\textit{nums}$,对于当前遍历到的元素 $\textit{nums}[i]$,如果在哈希表中存在,并且下标与当前元素的下标之差不超过 $k$,则返回 $\text{true}$,否则将当前元素加入哈希表中。

遍历结束后,返回 `false`
遍历结束后,返回 $\text{false}$

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -195,13 +195,12 @@ class Solution {
* @return Boolean
*/
function containsNearbyDuplicate($nums, $k) {
$hashtable = [];
for ($i = 0; $i < count($nums); $i++) {
$tmp = $nums[$i];
if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) {
$d = [];
for ($i = 0; $i < count($nums); ++$i) {
if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) {
return true;
}
$hashtable[$tmp] = $i;
$d[$nums[$i]] = $i;
}
return false;
}
Expand Down
17 changes: 8 additions & 9 deletions solution/0200-0299/0219.Contains Duplicate II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ tags:

### Solution 1: Hash Table

We use a hash table $d$ to store the nearest index of the number it has visited.
We use a hash table $\textit{d}$ to store the recently traversed numbers and their corresponding indices.

We traverse the array `nums`. For the current element $nums[i]$, if it exists in the hash table, and the difference between its index and the current index is no larger than $k$, then return `true`. Otherwise, we add the current element into the hash table.
Traverse the array $\textit{nums}$. For the current element $\textit{nums}[i]$, if it exists in the hash table and the difference between the indices is no more than $k$, return $\text{true}$. Otherwise, add the current element to the hash table.

After the traversal, return `false`.
After traversing, return $\text{false}$.

The time complexity is $O(n)$ and the space complexity is $O(n)$. Here $n$ is the length of array `nums`.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -194,13 +194,12 @@ class Solution {
* @return Boolean
*/
function containsNearbyDuplicate($nums, $k) {
$hashtable = [];
for ($i = 0; $i < count($nums); $i++) {
$tmp = $nums[$i];
if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) {
$d = [];
for ($i = 0; $i < count($nums); ++$i) {
if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) {
return true;
}
$hashtable[$tmp] = $i;
$d[$nums[$i]] = $i;
}
return false;
}
Expand Down
11 changes: 5 additions & 6 deletions solution/0200-0299/0219.Contains Duplicate II/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ class Solution {
* @return Boolean
*/
function containsNearbyDuplicate($nums, $k) {
$hashtable = [];
for ($i = 0; $i < count($nums); $i++) {
$tmp = $nums[$i];
if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) {
$d = [];
for ($i = 0; $i < count($nums); ++$i) {
if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) {
return true;
}
$hashtable[$tmp] = $i;
$d[$nums[$i]] = $i;
}
return false;
}
}
}