Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1122bc

Browse files
feat: update solutions to lc problem: No.0219 (doocs#4001)
No.0219.Contains Duplicate II
1 parent f42c382 commit a1122bc

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

‎solution/0200-0299/0219.Contains Duplicate II/README.md‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ tags:
6060

6161
### 方法一:哈希表
6262

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

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

67-
遍历结束后,返回 `false`
67+
遍历结束后,返回 $\text{false}$
6868

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

7171
<!-- tabs:start -->
7272

@@ -195,13 +195,12 @@ class Solution {
195195
* @return Boolean
196196
*/
197197
function containsNearbyDuplicate($nums, $k) {
198-
$hashtable = [];
199-
for ($i = 0; $i < count($nums); $i++) {
200-
$tmp = $nums[$i];
201-
if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) {
198+
$d = [];
199+
for ($i = 0; $i < count($nums); ++$i) {
200+
if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) {
202201
return true;
203202
}
204-
$hashtable[$tmp] = $i;
203+
$d[$nums[$i]] = $i;
205204
}
206205
return false;
207206
}

‎solution/0200-0299/0219.Contains Duplicate II/README_EN.md‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ tags:
5959

6060
### Solution 1: Hash Table
6161

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

64-
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.
64+
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.
6565

66-
After the traversal, return `false`.
66+
After traversing, return $\text{false}$.
6767

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

7070
<!-- tabs:start -->
7171

@@ -194,13 +194,12 @@ class Solution {
194194
* @return Boolean
195195
*/
196196
function containsNearbyDuplicate($nums, $k) {
197-
$hashtable = [];
198-
for ($i = 0; $i < count($nums); $i++) {
199-
$tmp = $nums[$i];
200-
if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) {
197+
$d = [];
198+
for ($i = 0; $i < count($nums); ++$i) {
199+
if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) {
201200
return true;
202201
}
203-
$hashtable[$tmp] = $i;
202+
$d[$nums[$i]] = $i;
204203
}
205204
return false;
206205
}

‎solution/0200-0299/0219.Contains Duplicate II/Solution.php‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ class Solution {
55
* @return Boolean
66
*/
77
function containsNearbyDuplicate($nums, $k) {
8-
$hashtable = [];
9-
for ($i = 0; $i < count($nums); $i++) {
10-
$tmp = $nums[$i];
11-
if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) {
8+
$d = [];
9+
for ($i = 0; $i < count($nums); ++$i) {
10+
if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) {
1211
return true;
1312
}
14-
$hashtable[$tmp] = $i;
13+
$d[$nums[$i]] = $i;
1514
}
1615
return false;
1716
}
18-
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /